Friday 19 March 2010

MS Enterprise Library – Rolling Flat File Trace Listener

 

This article describes the RollingFileTraceListener extension of the Logging block of the Microsoft Enterprise Library. This is a custom trace listener that can be plugged into the Enterprise Library Logging block like the standard trace listeners that are included. The FlatFileTraceListener shipped with EntLib 2.0 may not be adequate for enterprise systems since log files by default will continue to grow unchecked. This new trace listener provides support for rolling over log files based on age of the log file or file size.

Step 1 : 

In order to use the new trace listener from the Enterprise Library Configuration tool, you must refererence the following 2 assemblies.

Microsoft.Practices.EnterpriseLibraryExtensions.Logging.dll

Microsoft.Practices..EnterpriseLibraryExtensions.Logging.Configuration.Design.dll.

Step 2:

Open web.confgi file in Enterprise Library Configuration tool.

Add new Logging Application Block By Clicking on the root node.

image

Add the new Text Formater by Right Clicking on Formater section.

 

image

Now Add New Trace Lister in Logging Application Block By right Clicking on ‘Trace Listener’.

 

image

Specify the info as follows:

 

image

 

Now Add the Rolling flat file listener to category Sources –> General and Special Sources –> All Events by right clicking on them and adding the listener name.

 

image

 

STEP 4: Code to write errors to this db log:

Add a default aspx page to the website and write the following code in .cs file.

 

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            int i = 1;
            int j = 0;
            i = i / j;
        }
        catch (System.DivideByZeroException ex)
        { 
         }
    }
}

No comments:

Post a Comment