DCSIMG
Building a Simple Logging Http Module with Logging Application Block - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Building a Simple Logging Http Module with Logging Application Block

Building a Simple Logging Http Module with Logging Application Block

I wanted to Building a Simple Logging Http Module with Logging Application Block
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 a post 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 log details when a web request start
and when web request end:

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
start or end.

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
.

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# May 21, 2010 10:30 PM

Twitter Trackbacks for Building a Simple Logging Http Module with Logging Application Block - Gil Fink on .Net [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Building a Simple Logging Http Module with Logging Application Block - Gil Fink on .Net         [microsoft.co.il]        on Topsy.com

# May 27, 2010 4:21 PM