Friday 9 November 2012

Trace logging with the Enterprise Library for MS Dynamics CRM 2011 plug-ins - part 1

I really like the Enterprise Library for Trace logging and had used it in the past for trace logging of plug-ins, but the way it had done it was by passing the location of a config file on the unsecure configuration of each plug-in step, then loading that config file and using it.

In this post I discuss a simpler approach, which is this. Just add the logging elements to the two config files:
web.config and CrmAsyncService.exe.config. The former can be found in X:\Program Files\Microsoft Dynamics CRM\CRMWeb\ whereas the latter can be found in X:\Program Files\Microsoft Dynamics CRM\Server\bin. Since the latter is shorter, it's shown below:

A few comments about the file:
The EL section starts after the runtime element and it will log everything to the event log. This is probably a bad idea and I will provide a better sample in part 2 of this series.
Logging is only done for two categories, Trace and Error, which is probably fine for most applications.
The formatter is probably excessive, but I've not had the time to look at what is really needed.

 <?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <runtime> 
     <gcServer enabled="true"/>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
       <dependentAssembly>
         <assemblyIdentity name="Microsoft.Crm.Sdk" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
         <bindingRedirect oldVersion="4.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
       </dependentAssembly>
     </assemblyBinding>
   </runtime>
 <configSections>
 <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
 </configSections>
 <loggingConfiguration name="" tracingEnabled="true" defaultCategory="Trace">
     <listeners>
       <add name="Event Log Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
            listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
            source="Enterprise Library Logging" formatter="Text Formatter"/>
     </listeners>
     <formatters>
       <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" template="Timestamp: {timestamp}{newline}
 Message: {message}{newline}
 Category: {category}{newline}
 Priority: {priority}{newline}
 EventId: {eventid}{newline}
 Severity: {severity}{newline}
 Title:{title}{newline}
 Machine: {localMachine}{newline}
 App Domain: {localAppDomain}{newline}
 ProcessId: {localProcessId}{newline}
 Process Name: {localProcessName}{newline}
 Thread Name: {threadName}{newline}
 Win32 ThreadId:{win32ThreadId}{newline}
 Extended Properties: {dictionary({key} - {value}{newline})}" name="Text Formatter"/>
       <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.BinaryLogFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Binary Log Message Formatter"/>
     </formatters>
     <categorySources>
       <add switchValue="All" name="Trace">
         <listeners>
           <add name="Event Log Trace Listener"/>
         </listeners>
       </add>
       <add switchValue="All" name="Error">
         <listeners>
           <add name="Event Log Trace Listener"/>
         </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="Event Log Trace Listener"/>
         </listeners>
       </errors>
     </specialSources>
   </loggingConfiguration>
  </configuration>
At this point it is important to point out that the EL library files need to be copied to these locations as otherwise you will get issues when logging anything:
X:\Program Files\Microsoft Dynamics CRM\CRMWeb\bin
X:\Program Files\Microsoft Dynamics CRM\Server\bin.
 The files are the following:
Microsoft.Practices.EnterpriseLibrary.Common.dll
Microsoft.Practices.EnterpriseLibrary.Data.dll
Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll
Microsoft.Practices.EnterpriseLibrary.Logging.dll
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll
Strictly speaking the Microsoft.Practices.EnterpriseLibrary.Logging.Database.dll library is not needed now, but it might come in handy latter.

See part two here

No comments:

Post a Comment