Logging from a custom HTTP module

Some developers like to gather all logging into a single module. An example of this would be to log to multiple log destinations and maybe even enrich log messages to multiple loggers with the same info. We always recommend using the modules and handlers that come with ELMAH. But in case you want to log from a module manually, here's the recipe:

public class CustomLoggingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.Error += Application_Error;
    }

    public void Application_Error(object sender, EventArgs messageData)
    {
        HttpApplication application = (HttpApplication)sender;
        var context = application.Context;
        var error = new Error(application.Server.GetLastError(), context);
        var log = ErrorLog.GetDefault(context);
        log.Log(error);
    }

    public void Dispose()
    {
    }
}

In the example, I've created a new module named CustomLoggingModule. The module needs to be configured in web.config as explained here. When starting up the application, ASP.NET calls the Init-method. In this method, an Error event handler is set. Every time a new error is happening in your web application, ASP.NET now calls the Application_Error-method. In this method, I wrap the last thrown error in ELMAH's Error object and log it through the ErrorLog class.

Be aware that logging errors this way, disables ELMAH's built-in events like filtering.


This article was brought to you by the elmah.io team. elmah.io is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening, and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website