Errors Http Module

The following http module allows your web application to log unhandled exceptions using OpenWaves.Log service.

using System;
using System.Web;
using OpenWaves;
...
public class ErrorsHttpModule : IHttpModule
{
    public void Dispose()
    {            
    }

    public void Init(HttpApplication context)
    {
        context.Error += OnError;
    }

    private void OnError(object sender, EventArgs eventArgs)
    {
        var exception = HttpContext.Current.Server.GetLastError();

        if (exception != null)
        {
            var httpException = exception as HttpException;
            if (httpException != null && httpException.GetHttpCode() == 404)
                return;

            Log.Error(exception, "Unhandled exception has occured.");
        }
    }
}
By default it has simple filter on http code and throws out classic 'not found' error. You can modify this behaviour by providing custom implementation of OnError method.

Usage example

To use ErrorsHttpModule in your web application you should modify web.config file and provide httpModules or modules XML element. The following example adds ErrorsHttpModule to ASP .NET application hosted on IIS version 5.0, 5.1, or 6.0. Let's assume that your project name is WebApplication and your root namespace is WebApplication.

<configuration>
  <system.web>
    <httpModules>
      <add name="ErrorsHttpModule" type="WebApplication.ErrorsHttpModule, WebApplication" />
    </httpModules>
  </system.web>
</configuration>
The same configuration for IIS 7.0 version looks like this:
<configuration>
  <system.webServer>
    <modules>
      <add name="ErrorsHttpModule" type="WebApplication.ErrorsHttpModule, WebApplication" />
    </modules>
  </system.webServer>
</configuration>
Installing OpenWaves.Templates.Errors package by http://nuget.org you don't have to worry about anything. NuGet package manager will apply web.config.transform file and modify your web.config for you.

Last edited Jan 25, 2013 at 1:40 PM by piotrdela, version 17

Comments

No comments yet.