Enterprise Library: Logging Application Block 5.0 in ASP.NET

In this article you will learn how to use Logging Application Block with a real-world example in ASP.NET.

Why use Logging Application Block? Developers frequently write applications that require logging functionality. Typically, these applications format and log information in response to application events. For example, developers often write code to log information in response to unexpected conditions, such as an application exception, or failure to connect to a database. Developers also write code to trace application flow through components during the execution of an application use case or scenario. The Enterprise Library Logging Application Block simplifies the implementation of common logging functions. You can use the Logging Application Block to write information to a variety of locations:

  • The event log
  • An e-mail message
  • A database
  • A message queue
  • A text file
  • A Windows® Management Instrumentation (WMI) event
  • Custom locations using application block extension points

Read more at: http://msdn.microsoft.com/en-us/library/ff664569(v=pandp.50).aspx

Install Enterprise Library

Please follow this link to download the Enterprise Library:

http://www.microsoft.com/en-in/download/details.aspx?id=15104

Getting Started

Begin using the following procedure:

  • Start Visual Studio
  • Create a new website
  • Provide the name and location of website
  • Click "Next"

Now add a reference for the following two assemblies in the bin folder: "Microsoft.Practices.EnterpriseLibrary" and "Logging.dll".

Now open the web.config file in edit mode and edit the logging settings.

img1.jpg

Image 1.

First of all add the Logging setting block.

img2.jpg

Image 2.

Now delete the existing event log listener.

img3.jpg

Image 3.

And add a new flat file trace listener.

img4.jpg

Image 4.

Now provide the following details, like formatter name, header footer message and trace output options and in the general tab select the flat file trace listener and in the logging errors and warnings, select the flat file trace listener and click "Save".

img5.jpg

Image 5.

Now check the updated web.config.

<configSections>

    <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />

  </configSections>

  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">

    <listeners>

      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        fileName="trace.log" formatter="Text Formatter" />

    </listeners>

    <formatters>

      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

        template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"

        name="Text Formatter" />

    </formatters>

    <categorySources>

      <add switchValue="All" name="General">

        <listeners>

          <add name="Flat File Trace Listener" />

        </listeners>

      </add>

    </categorySources>

    <specialSources>

      <allEvents switchValue="All" name="All Events" />

      <notProcessed switchValue="All" name="Unprocessed Category" />

      <errors switchValue="All" name="Logging Errors &amp; Warnings">

        <listeners>

          <add name="Flat File Trace Listener" />

        </listeners>

      </errors>

    </specialSources>

  </loggingConfiguration>

To test whether it's working write some code in the page.

protected void Button1_Click(object sender, EventArgs e)

    {

        LogEntry logEntry = new LogEntry();

        logEntry.EventId = 100;

        logEntry.Priority = 2;

        logEntry.Message = "message by Raj Kumar";

        Logger.Write(logEntry);  

    }

Now run the application to see the output.

As you will see, there is a trace, "trace.log", that has been created in your application root, now open it.

img6.jpg

Image 6.

Up Next
    Ebook Download
    View all
    Learn
    View all