Enterprise Library Logging Sample

Using Enterprise Library (still on 5), You can declaratively configure the logger properties (including desired formatting, see Textformatter template below)) in the app.config’s appsettings:

  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
  defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">   <listeners>    <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    source="Enterprise Library Logging" formatter="Text Formatter 2"
    log="" machineName="." traceOutputOptions="None" />    <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    fileName="%AppData%\trpsoft\langlabemailer\trace-rolling.log"
    footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment"
    rollInterval="Day" rollSizeKB="1000" maxArchivedFiles="10" traceOutputOptions="None" />    <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    fileName="%AppData%\trpsoft\langlabemailer\exception.log" header=""
    footer="" formatter="Text Formatter" traceOutputOptions="None" />    <add name="Rolling Flat File Trace Listener 2" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    fileName="%AppData%\trpsoft\langlabemailer\exception-rolling.log"
    footer="" formatter="Text Formatter" header="" rollFileExistsBehavior="Increment"
    rollInterval="Hour" rollSizeKB="100" maxArchivedFiles="10" filter="All" />   </listeners>   <formatters>    <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    template="Timestamp	 {timestamp}	Message	 {message}	Category	 {category}	Priority	 {priority}	EventId	 {eventid}	Severity	 {severity}	Title	{title}	Machine	 {localMachine}	App Domain	 {localAppDomain}	ProcessId	 {localProcessId}	Process Name	 {localProcessName}	Thread Name	 {threadName}	Win32 ThreadId	{win32ThreadId}	Extended Properties	 {dictionary({key} - {value})}"
    name="Text Formatter" />    <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.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 2" />   </formatters> 
   <categorySources>    <add switchValue="All" name="General">     <listeners>      <add name="Rolling Flat File Trace Listener" />     </listeners>    </add>    <add switchValue="All" name="Exceptions">     <listeners>      <add name="Event Log Listener" />      <add name="Rolling Flat File Trace Listener 2" />     </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 Listener" />     </listeners>    </errors>   </specialSources>  </loggingConfiguration>  <exceptionHandling>   <exceptionPolicies>    <add name="Log and Rethrow">     <exceptionTypes>      <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      postHandlingAction="NotifyRethrow">       <exceptionHandlers>        <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        logCategory="Exceptions" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
        formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
        priority="0" />       </exceptionHandlers>      </add>     </exceptionTypes>    </add>   </exceptionPolicies>  </exceptionHandling>  <appSettings> 

Import and call the logger like so:

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging;  

using Microsoft.Practices.EnterpriseLibrary.Logging;  

Logger.Write("regex:RegExRecordingFileGroup - target:" + "\t" + _filenamenoext + "\t" + strGroups); 

Which outputs: image and

image,

the  latter can be easily imported and analyzed in MS-Excel:

image

These are obviously only the simplest examples, study the Enterprise Library documentation for more customization

  1. No comments yet.
  1. No trackbacks yet.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.