Monday 12 November 2012

Trace logging with the Enterprise Library for MS Dynamics CRM 2011 plug-ins (using MSMQ) - part 4

In part one of this series I described how to use the enterprise library for trace logging in MS Dynamics CRM 2011 plug-ins (it should also work for MS Dynamics CRM 4.0) and in part two, I described how to use add the necessary changes to the various configuration files using Wix, in the third post, I provided a small code sample of how to use the enterprise library.

In this fourth and final post I describe a rather interesting use of the Enterprise Library trace logging capabilities, feel free to contact me if you want a sample.

From version 4.0 of the enterprise library, not sure whether present in any other versions, there is the MSMQ Distributor Service, which picks up queue messages and then logs them to any trace listener. This means that something like this is possible (A picture is worth a thousand words and all that):

Pilfered from here
In our environment we will have four application servers running MS Dynamics CRM 2011 and we want to log everything to a database on the SQL Cluster that hosts the Dynamics CRM databases. For the web services we are logging directly to the database, but for the plug-ins and code based workflows we will be using MSMQ trace listeners to log to a private queue on the SQL Cluster and then have the MSMQ distributor service do the final mile of logging to the database.

Although the queue is private, Everyone will be able to write messages to it, which means that the plug-ins that run under the user context will be able to write messages to the queue or in other words, trace logging should work for all users regardless of their permissions, which is the key. We could log to the database with a sql user but that is not what we wanted.

I will not post the configuration file logging section that I used again, as it's already been posted here.
  1. Install MSMQ on all servers (Application and Database), which can be done using the script posted here
  2. Use the configuration file logging section posted here for the CrmAsyncService.exe.config and web.config. 
  3. Install MSMQ Distributor Service on Database Server.
    Installutil -i MsmqDistributor.exe
  4. Change MSMQ Distributor Service configuration, see sample below.
  5. Start MSMQ Distributor Service, e.g.:
    net start "Enterprise Library Distributor Service"
MSMQ Distributor Service Sample Configuration File.
 <?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <configSections>
     <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging" />
     <section name="msmqDistributorSettings" type="Microsoft.Practices.EnterpriseLibrary.Logging.MsmqDistributor.Configuration.MsmqDistributorSettings, MsmqDistributor"/>
   </configSections>
   <loggingConfiguration tracingEnabled="true" defaultCategory="Trace" logWarningsWhenNoCategoriesMatch="true">
     <logFilters/>
     <categorySources>
       <add name="Trace" switchValue="All">
         <listeners>
           <add name="Database Trace Listener"/>
         </listeners>
       </add>
       <add name="Error" switchValue="All">
         <listeners>
           <add name="Database Trace Listener"/>
         </listeners>
       </add>
     </categorySources>
     <specialSources>
       <allEvents name="allEvents" switchValue="All"/>
       <notProcessed name="notProcessed" switchValue="All"/>
       <errors switchValue="All" name="Logging Errors &amp; Warnings">
         <listeners>
           <add name="Formatted EventLog TraceListener" />
         </listeners>
       </errors>
     </specialSources>
     <listeners>
       <add name="Formatted EventLog TraceListener"
         type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
         listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
         source="Enterprise Library Logging" formatter="Text Formatter"/>
       <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
         databaseInstanceName="loggingdb" writeLogStoredProcName="WriteLog"
         addCategoryStoredProcName="AddCategory" formatter="Text Formatter" />
     </listeners>
     <formatters>
       <add name="Text Formatter"
         type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging"
         template="Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}Priority: {priority}{newline}EventId: {eventid}{newline}Severity: {severity}{newline}Title:{title}{newline}Machine: {machine}{newline}App Domain: {appDomain}{newline}ProcessId: {processId}{newline}Process Name: {processName}{newline}Thread Name: {threadName}{newline}Win32 ThreadId:{win32ThreadId}{newline}Extended Properties: {dictionary({key} - {value}{newline})}" />
     </formatters>
   </loggingConfiguration>
   <connectionStrings>
     <add name="loggingdb" connectionString="Data Source=sql2k12a;Initial Catalog=Logging;Integrated Security=SSPI;"
       providerName="System.Data.SqlClient" />
   </connectionStrings>
   <msmqDistributorSettings
                 msmqPath=".\Private$\logging"
                 queueTimerInterval="1000"
                 serviceName="Enterprise Library Distributor Service" />
 </configuration>

1 comment:

  1. Great!

    any updates for ent.lib 6.0?
    custom email trace listener for send email using HTML template ? thx

    ReplyDelete