How to use Logging Application Block in Enterprise Library 5.0

Posted by in .NET Framework category on for Intermediate level | Points: 250 | Views : 85095 red flag
Rating: 4.56 out of 5  
 16 vote(s)

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

<head runat="server">
    <title> Event Viewer</title>
    <form id="form1" runat="server">
    <asp:Button ID="btn1" runat="server" Text="Submit" onclick="btn1_Click" />


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";

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.


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.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Nitha Deepak
Member Level: Bronze
Member Status: Member
Member Since: 4/21/2011 4:47:37 AM
Country: India
Nitha Deepak

Login to vote for this post.

Comments or Responses

Posted by: Teena on: 4/27/2011 | Points: 25
Very Informative....
Posted by: Nithadeepak on: 4/27/2011 | Points: 25
Thanks Teena

Nitha Deepak
Posted by: Ponnu on: 4/27/2011 | Points: 25
Informative one and interesting too..
Posted by: Susanthampy on: 4/27/2011 | Points: 25
Posted by: Nithadeepak on: 4/27/2011 | Points: 25
thnks to Ponnu and Susan.
Nitha Deepak
Posted by: Bijub on: 4/27/2011 | Points: 25
good article and is very informative..
Posted by: Nithadeepak on: 4/27/2011 | Points: 25
thanks Biju
Posted by: Ravi_pinnoju on: 4/27/2011 | Points: 25
Very nice article and easy to implement this concept through this article.
Posted by: Nithadeepak on: 4/27/2011 | Points: 25
Thanks Ravi

Posted by: Tripati_tutu on: 4/28/2011 | Points: 25
Good one...
Posted by: Nithadeepak on: 5/2/2011 | Points: 25
Thanks Tripati_tutu

Posted by: Noshirwan on: 5/6/2011 | Points: 25
Nice article
Posted by: Noshirwan on: 5/6/2011 | Points: 25
Hi Nitha,
Thanks for the article. There is one issue though with the Trace Listner(second part). Even though it lets me 'Add' a listener as a 'Trade Listener' it shows . It shows it in a red box which has a tool tip message:-'Reference Item is not available. Select a new item or add a new item to logging Target Listeners'. The second issue is in the images you have posted you have got 'all Trace Listeners' selected; I don't see that.
Your response is appreciated.
Posted by: Noshirwan on: 5/6/2011 | Points: 25
Okay I got.
Thanks anyway
Posted by: Vijarvind on: 5/13/2011 | Points: 25
Hi Nitha,

The article if very informative.

I am facing one issue while using trace.log file logging the exceptions. if you can please help.
I need to get the stack trace with other exception information. But stack trace is not available with provide enterprise library 5.0 exception logging template. Is there any way I can add this.

Also I want to genrate seprate trace.log file for every day.

Posted by: Nithadeepak on: 5/17/2011 | Points: 25
Hi Vijarvind,

I'll try. If you solved your issue please share it with us also.
Posted by: Nithadeepak on: 5/17/2011 | Points: 25
Thanks to Noshiwan and vijarvind.

Posted by: Vinaym on: 5/18/2011 | Points: 25
Good One
Posted by: Nithadeepak on: 5/18/2011 | Points: 25
Thanks vinay......
Posted by: Nissan on: 5/20/2011 | Points: 25
This is a good article. Thank you. Can you also share some info regarding logging to the database. Is your sample working on Windows 7 machine? When I installed Enterprise Library it installed in Program Files(x86) path. An got an error saying I cannot access the ExceptionPlolicy.cs at this location. Any one have similar issues?
Posted by: Nithadeepak on: 5/23/2011 | Points: 25
Hi Nissan,
Thanks for your comments...
Please use the below link to download Hands-on labs for Microsoft Enterprise Library 5.0. It will be very useful to understand all the application blocks.
Posted by: Ninilcr on: 6/2/2011 | Points: 25

Wow.This is a really good article .I have some doubts on this but first i will try my level best and if i am not able to solve it then i will need your expert help. Anyways, thanks for this post .Good luck to you and pls keep posting .

Posted by: Nithadeepak on: 6/14/2011 | Points: 25
Thanks Ninil
Posted by: Jayeshl on: 6/17/2011 | Points: 25
thanks friend to write this ..

Jayesh L
Posted by: srilakshmivaranasi123-10757 on: 6/23/2011 | Points: 25
realy good artical......
Posted by: akkalaswetha-13069 on: 12/21/2011 | Points: 25
Hi , I am using the same process that you have done above , but trace.log file is not been created in my application!!!
can you please help me out with this

Posted by: Nithadeepak on: 12/29/2011 | Points: 25
Hi Swetha,
Refresh the solution explorer to see the trace.log file.

Posted by: Abhiigate on: 4/30/2012 | Points: 25
This is really nice article for beginners.
Can u please provide the articles for other application blocks also?

Posted by: Nithadeepak on: 4/30/2012 | Points: 25
Posted by: Dragontl21 on: 9/10/2012 | Points: 25
Can you show me how to apply this logging method into MVC3?
Posted by: abritman-10469 on: 11/27/2012 | Points: 25
Hi ,
I am not getting these Design.
Can you provide sample configuration for MailLogging..!!
can we use it for gmail Smtp ..?

Login to post response

Comment using Facebook(Author doesn't get notification)