logging the user identity

Topics: Logging Application Block, Policy Injection Application Block
Feb 3, 2009 at 9:42 AM
Hi all,
i want to log the user identity (thread current principle, windows identity wcf security context) into a text file.
i have a wcf service which is wrapped by policy injection to log all service method calls. the log is a flat text file.

im not able to log the user identity, non of the formatter template properties works,
please advice

here is the configuration relevant sections:
<policyInjection>
    <policies>
      <add name="Policy">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.MemberNameMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="Member Name Matching Rule">
            <matches>
              <add match="Echo" ignoreCase="false" />
            </matches>
          </add>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.TypeMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="Type Matching Rule">
            <matches>
              <add match="EchoService" ignoreCase="false" />
            </matches>
          </add>
        </matchingRules>
        <handlers>
          <add logBehavior="Before" beforeMessage="Before Echo" afterMessage="After Echo"
            eventId="52" includeParameterValues="true" includeCallStack="false"
            includeCallTime="true" priority="-1" severity="Verbose" order="0"
            type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
            name="Logging Handler">
            <categories>
              <add name="General" />
            </categories>
          </add>
        </handlers>
      </add>
    </policies>
  </policyInjection>
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true"
    revertImpersonation="false">
    <listeners>
      <add fileName="trace.log" header="----------------------------------------"
        footer="----------------------------------------" formatter="Text Formatter"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Formatted EventLog TraceListener" />
    </listeners>
    <formatters>
      <add template="&#xD;&#xA;MIdentity: {windowsIdentity}&#xD;&#xA;           &#xD;&#xA;Timestamp: {timestamp}&#xD;&#xA;           &#xD;&#xA;Message: {message}&#xD;&#xA;           &#xD;&#xA;Category: {category}&#xD;&#xA;           &#xD;&#xA;Priority: {priority}&#xD;&#xA;           &#xD;&#xA;EventId: {eventid}&#xD;&#xA;           &#xD;&#xA;Severity: {severity}&#xD;&#xA;           &#xD;&#xA;Title:{title}&#xD;&#xA;           &#xD;&#xA;Machine: {machine}&#xD;&#xA;           &#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;           &#xD;&#xA;Process Id: {processId}&#xD;&#xA;           &#xD;&#xA;Process Name: {processName}&#xD;&#xA;           &#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;           &#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;           &#xD;&#xA;Calling Method: {property(TypeName)}&#xD;&#xA;           &#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Formatted EventLog TraceListener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
Feb 3, 2009 at 10:43 AM
Edited Feb 3, 2009 at 11:00 AM
Hi,

You try using the TextFormatter and just edit the template: 

In the application code:

IDictionary<string, object> properties = new Dictionary<string, object>();
properties.Add("MyKey", "MyValue");
Logger.Write("Hello", properties);

In the Template Editor of the Text Formatter I added this:

MyKey: {keyvalue(MyKey)}


This will result in the event log like this:
MyKey: MyValue


Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Feb 3, 2009 at 11:46 AM
Hi,
the thing is, i don't use Logger.Write(...), i use the policy injection to wrap each service call for automatic logging

is there a way to get the identity in that way?

Feb 4, 2009 at 3:53 AM
You can't directly pass it to the handler so the way to go is to create your own custom trace listener which would add the dictionary containing your custom entries.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 4, 2009 at 7:06 AM
Sara, thank you
you have confirmed my suspicions: the only solution i found was to write my own listener, give it my default formatter (defined in app.config) and then implement my own log handler (either database or file...)

thanks