How to use Enterprise Library for Logging

In this article,  I am going to show you how Logging Application Block  are used for logging the information to different source.

See the below steps.

Step 1. Download the Enterprise Library 5.0, from the below url

http://www.microsoft.com/downloads/en/details.aspx?FamilyId=bcb166f7-dd16-448b-a152-9845760d9b4c

Step 2. Install the Enterprise Library once downloaded.

Step 3. Once install successfully, when you open the web/window application and write click on config file, you can see the below menu(Edit Enterprise library..) to edit the config file.

Click on the menu(Edit Enterprise library..) to edit  the config file.

Step 4.  Once click you can see the below screen

now click on the Blocks menu and select Logging Setting to add Logging application block.

Once  click on Add Logging Setting you can see the below screen.

Step 4.  Just save the config file by clicking File->Save.

And  add the below code in your program and execute the code, by default it log the message in the Event Log.

LogEntry logEntry = new LogEntry();

logEntry.EventId = 200;

logEntry.Priority = 1;

logEntry.Message = “Error Occured”;

logEntry.Severity = TraceEventType.Error;

Logger.Write(logEntry);

After executing this code you can check the Event Log and see the message.

Log message in the Text File

Step 5.  Click on the plus sign in front on Logging Target Listeners and click on the Add Flat File Trace Listener.


Now

–          Select Formatter to Text Formatter in the Flat file Trace Listener,

–          Select Listeners Name to Flat File Trace Listener in General  and Logging Error & Warning Section.

Once save the config file, logging will be started at Text file.

Conclusion:

Using Enterprise Library, without changing code the log has been maintained to either Event Log, Flat file or Email etc. using this library message can be log in different source as below..

–          The event log

–          An e-mail message

–          A database

–          A message queue

–          A text file

–          A WMI event

–          Custom locations using application block extension points

It simplify the life of programmer to use common and standard function for logging message by maintaining consist approach the practice.

References:

http://www.eduweb.vic.gov.au/devreskit/appdev/Application%20Blocks/Logging/logging-standards-details.htm

Advertisements

10 thoughts on “How to use Enterprise Library for Logging

  1. Hi,
    this is good article but here i am facing problem.. while implementing Exception block in class library its give error
    Activation error occured while trying to get instance of type LogWriter, key “”

    Regards
    Brijesh

    • Hi Sanjiv,

      Really this is very useful artical, this artical has saved my time.
      if you have created artical for exception handeling in enterprise library 5 then please share.

      Thanks.
      Tejesh

  2. This works fine when I run the application on my local machine. But once deployed to the server, it no longer works. What kind of file permission settings that I need to use in the server?

      • All the Enterprise library dlls are in the server. Also, I have logged into the server machine and after an error, the error was added to the Error.log file. But when I try to access the same server url from my machine, the Error log file is not getting updated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s