Logging with Microsoft Enterprise Library 4.1

Original author: David Starr
  • Transfer
This article describes how to work with the logging unit from the Microsoft Enterprise Library 4.1. I decided to figure it out after visiting Patterns & Practices Roadmap Kiev .

Install Enterprise Library


Download Enterprise Library 4.1 from here . Remember that in addition to the logging unit, many other components are installed.


Creating a new project in Visual Studio


Open Viual Studio, create a new console application and name it, for example, HelloWorldEntLibLogging .

Next, you need to add a reference to the library:

image

Logging Configuration


Most of the EntLib functionality is configured, including a logging unit. You need to add a configuration file to the project and add information using the add-on for Visual Studio, which was installed with the library.

Using the Solution Explorer, add a new Application Configuration File, leave the default file name app.config:

image

Next, right-click on app.config and select Edit Enterprise Library Configuration:

image

In the opened editor, select New | Logging Application Block:

image

A new Logging Application Block should appear in the editor tree:

image

Close the editor, open app.config:

  1:
  2:
  3:  
  4:    

  5:    

  6:  
  7:     8:     defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
  9:    
 10:        11:         log="Application" machineName="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
 12:         traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
 13:         name="Formatted EventLog TraceListener" />
 14:    

 15:    
 16:        17:         type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
 18:         name="Text Formatter" />
 19:    

 20:    
 21:      
 22:        
 23:          
 24:        

 25:      

 26:    

 27:    
 28:      
 29:      
 30:      
 31:        
 32:          
 33:        

 34:      

 35:    

 36:  

 37:

* This source code was highlighted with Source Code Highlighter.

There are many different settings in the file now, but they are not of interest to us at the moment.

Add the logging code to the application


Connect the namespace:

using Microsoft.Practices.EnterpriseLibrary.Logging;

* This source code was highlighted with Source Code Highlighter.

Next, add the following code to Program.cs:

class Program
{
  static void Main(string[] args)
  {
    LogEntry entry = new LogEntry()
               {
                 Message = "Hello Ent. Lib. Logging"
               };
   
    Logger.Write(entry);
  }
}

* This source code was highlighted with Source Code Highlighter.

That is all there is to it!

Launching the application and checking logging


We run the application, then run the Windows Event Viewer and watch the latest events. You should see the recorded information:

image

You can also specify Severity in your LogEntry :

LogEntry entry = new LogEntry()
           {
             Message = "Hello Ent. Lib. Logging",
             Severity = TraceEventType.Critical
           };

* This source code was highlighted with Source Code Highlighter.


If you run the application now, you will see a critical Windows Event Viewer error.

PS I’ll add from myself that in addition to the logging block, EntLib includes a large number of other blocks, including Caching, Cryptography, Data Access, Exception Handling, Logging, Policy Injection, Security, Validation and Unity. More details can be found at http://entlib.codeplex.com/ .

Also popular now: