Pages

Tuesday, March 20, 2012

How to use Logging Application Block in Enterprise Library 5.0

Here I'm writing about Logging Application Block of Enterprise Library 5.0. For using application blocks first you have to install Microsoft Patterns and Practices Enterprise Library 5.0. After installing this you will get an edit mode while right clicking the web.config from your application .

Different application blocks available in Enterprise Library 5.0 are:

  • Caching Application Block
  • Data Access Application Block
  • Exception Handling Application Block
  • Logging Application Block
  • Policy Injection Application Block
  • Cryptography Application Block
  • Security Application Block
  • Validation Application Block
The edit mode of web.config looks like the below picture.


By clicking on the blocks menu you can add the desired blocks into your application. So here I'm adding Logging Application Block into my application. So the web.config  looks like below


Use of Logging Application Block

Logging Application Block is used to write information’s to variety of locations like:

  • event log
  • an email
  • a database
  • a message queue
  • a text file
  • a WMI event
  • custom locations using application block extension points.

You can log messages to any locations by editing web.config without changing the c# codes.

Here I'm showing how to log information’s to an event log, an email and a text file.

1.Logging messages to event log (Event Viewer)

First you have to configure the web.config. By default the messages are logged into the event log. 


You can edit the web.config  Give the source name as the name of your application.


Below example shows how to log event viewer in a button click.

First you have to add the reference Enterprise Library Logging Application Block to the bin folder of your application.

aspx page

     Event Viewer
  
  
    
    
    

aspx.cs

using System;
using Microsoft.Practices.EnterpriseLibrary.Logging;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
       
        LogEntry logEntry = new LogEntry();
        logEntry.EventId = 100;
        logEntry.Priority = 2;
        logEntry.Message = "Informational message";
        Logger.Write(logEntry);   
    
    }
}


When you click on the button the message will be logged into the event viewer. Instead of using LogEntry class, you can  use only  the Write() method of the Logger class as shown below.

using System;
using Microsoft.Practices.EnterpriseLibrary.Logging;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        
        Logger.Write("Informational message","EventLog",2,100);
    }
}


2. Logging messages to an email

There is no need to change the c# code only configure the web.config. You have to change the logging target listener from event log listener to email trace listener. You can do this by clicking on the + sign on the right of logging target listeners.

You have to give the From Address, Smtp port, Smtp Server, To Address in the web.config.


While clicking the button the message will be logged to the email.

3. Logging messages to a text file

No need to change the c# code. Configure the web.config. Select Flat file trace listener as logging target listener. The web.config is shown below.


While clicking the button, a log file (here trace.log) will be created in your application and the message will be logged into that file.

Conclusion

Logging Application Block in the Enterprise Library is used to log messages to different locations by configuring the web.config, without any changes in the code.

No comments: