Click here to Skip to main content
Click here to Skip to main content

Building a Simple Logging HTTP Module with Logging Application Block

By , 21 Jul 2010
 

I wanted to check how to use the fluent configuration API for the Logging Application Block in Enterprise Library 5.
So I thought to myself, why not implement it with an HTTP module and provide two examples in one post.

Using the Fluent Configuration API with Logging Application Block

I wrote an article about the fluent configuration in Enterprise Library 5 in the past. Here is how I configured the Enterprise Library container in order to use the Logging Application Block in a simple flat file scenario:

private void ConfigureEnterpriseLibraryContainer()
{
  var builder = new ConfigurationSourceBuilder();
  builder.ConfigureInstrumentation().EnableLogging();
  builder.ConfigureLogging().WithOptions
         .LogToCategoryNamed("General")
           .WithOptions
           .SetAsDefaultCategory()
           .SendTo
           .FlatFile("Log File")
           .FormatWith(new FormatterBuilder()
           .TextFormatterNamed("Textformatter"))
               .ToFile("file.log");

  var configSource = new DictionaryConfigurationSource();
  builder.UpdateConfigurationWithReplace(configSource);
  EnterpriseLibraryContainer.Current =
    EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
}

Pay attention that in order to use this API, you need to add references to the following DLLs:

  • Microsoft.Practices.EnterpriseLibrary.Common
  • Microsoft.Practices.EnterpriseLibrary.Logging
  • Microsoft.Practices.ServiceLocation
  • System.Configuration

Now that we have the configuration placed in memory, we can use it in our code.

Building the HTTP Module

In order to build an HTTP module, we only need to implement the IHttpModule interface. The following is a simple LoggingHttpModule which logs details when a web request starts and when a web request ends:

using System;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace HttpModules
{
  public class LoggingHttpModule : IHttpModule
  {
    #region Members

    private LogWriter _writer;

    #endregion

    #region IHttpModule Members

    public void Dispose()
    {
      if (_writer != null)
      {
        _writer.Dispose();
      }
    }

    public void Init(HttpApplication context)
    {
      CreateLogWriter();
      context.BeginRequest += new EventHandler(context_BeginRequest);
      context.EndRequest += new EventHandler(context_EndRequest);
    }

    private void CreateLogWriter()
    {
      ConfigureEnterpriseLibraryContainer();
      _writer = EnterpriseLibraryContainer.Current.
                  GetInstance<LogWriter>();
    }

    private void ConfigureEnterpriseLibraryContainer()
    {
      var builder = new ConfigurationSourceBuilder();
      builder.ConfigureInstrumentation().EnableLogging();
      builder.ConfigureLogging().WithOptions
             .LogToCategoryNamed("General")
               .WithOptions
               .SetAsDefaultCategory()
               .SendTo
               .FlatFile("Log File")
               .FormatWith(new FormatterBuilder()
               .TextFormatterNamed("Textformatter"))
                   .ToFile("file.log");

      var configSource = new DictionaryConfigurationSource();
      builder.UpdateConfigurationWithReplace(configSource);
      EnterpriseLibraryContainer.Current =
        EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
      _writer.Write(new LogEntry
      {
        Message = "BeginRequest"
      });
    }

    void context_EndRequest(object sender, EventArgs e)
    {
      _writer.Write(new LogEntry
      {
        Message = "EndRequest"
      });
    }

    #endregion
  }
}

Using the Module

In order to use the module, all we need to do is to add a reference to the class library that holds the LoggingHttpModule. Then we need to register the module in the web.config file in the httpModules element like:

<httpModules>
    <add name="LoggingHttpModlue"
       type="HttpModules.LoggingHttpModule, HttpModules"/>
</httpModules>

That is it. Now the module will be executed whenever a request starts or ends.

Summary

Using the fluent configuration API of Enterprise Library 5 can make it easy to configure cross cutting concerns like logging without a configuration file. The API is simple and very easy to apply. In this post, I showed how to combine the Logging Application Block within an HTTP module with the fluent configuration API.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Gil Fink
Architect Sela Group
Israel Israel
Member
Gil Fink is an expert in ASP.NET and Microsoft data platform and serves as a Senior Architect at SELA Group. He is a Microsoft data platform MVP and a certified MCPD Enterprise Application Developer. Gil has worked in the past in variety of positions and projects as a leading developer, team leader, consultant and more. His interests include Entity Framework, Enterprise Library, WCF, LINQ, ADO.NET and many other new technologies from Microsoft.
 

My technical blog: http://www.gilfink.net

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to configure and use Microsoft Enterprise Library 5 for loggingmemberLibish Varghese Jacob17 Jul '12 - 20:21 
How to configure and use Microsoft Enterprise Library 5 for logging Find it here
My website
 
Regards,
Libish Varghese Jacob

Generalunable to find ConfigureLoggingmemberavirami13 Dec '10 - 18:36 
Hi,
 
was the API changed ?
 
I can find the function "ConfigureLogging" in ConfigurationSourceBuilder.
can you help ?
 
Thanks,
 
Aviram
GeneralRe: unable to find ConfigureLoggingmemberGil Fink14 Dec '10 - 3:51 
Hi Aviram,
 
The API wasn't chnaged.
You should add a reference to the Microsoft.Practices.EnterpriseLibrary.Logging assembly which contain the ConfigureLogging extension method. Then add a using statement for the assembly and you'll have the method.
 
I hope it will help you.
GeneralRe: unable to find ConfigureLoggingmemberavirami14 Dec '10 - 20:41 
Thanks ! it did.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 22 Jul 2010
Article Copyright 2010 by Gil Fink
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid