Recently we have started developing a number of MVC3 applications. They have all started to be released to our staging and production servers and for some reason we are no longer seeing the error emails / log we were seeing whilst developing. After a little investigation it appeared that Application_Error in Golbal.asax was not being fired and we were just seeing a nice friendly error page.

Method that works but isn’t ideal

Initially is commented out the HandleErrorAttribute filter in Global.asax and added a customError handler with defaultRedirect=”~/Error” in our web.config.


public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new log4netExceptionFilter()); //must be the first line
    filters.Add(new HandleErrorAttribute());
}

This seemed to make everything work ok. But after some consideration I decided this isn’t ideal because the correct http codes are never sent to the browser. So I decided to dig a little deeper.

So why doesn’t Application_Error fire?

The best explanation I found for this was on stack overflow: Application_Error not firing when customerrors = “On”. Specifically this excerpt:

By default (when a new project is generated), an MVC application has some logic in the Glabal.asax.cs file. This logic is used for mapping routes and registering filters. By default, it only registers one filter: a HandleErrorAttribute filter. When customErrors are on (or through remote requests when it is set to RemoteOnly), the HandleErrorAttribute tells MVC to look for an Error view and it never calls the Application_Error() method. I couldn’t find documentation of this but it is explained in this answer on programmers.stackexchange.com.

None of the solutions I saw in that post looked particularly appealing so I looked at how other error logging frameworks were used with MVC3. That is when I came across this solution using ELMAH: ASP.NET MVC Magical Error Logging with ELMAH. Specifically Step 4 where they create a class that implements IExceptionFilter.

I just created a similar class that worked with log4net and have been living the logging dream since.

Here is the full solution:

log4netExceptionFilter.cs


using System;
using System.Web.Mvc;

public class log4netExceptionFilter : IExceptionFilter
{
    public void OnException(ExceptionContext context)
    {
        Exception ex = context.Exception;
        if (!(ex is HttpException)) //ignore "file not found"
        {
            Logging.PutError("Unhandled exception error", ex);
        }
    }
}

Loggin.cs


using System;
using System.Configuration;
using log4net;
using log4net.Config;

public static class Logging
{
    private static readonly ILog Log = LogManager.GetLogger(ConfigurationManager.AppSettings["log4netLogger"]);

    public static void PutError(string message, Exception e)
    {
        Log.Error(message + "; Url: " + HttpContext.Current.Request.Url.AbsoluteUri + "; Error: ", e);
    }

    // Other Custom Logging Functions

}

Global.asax.cs

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new log4netExceptionFilter()); //must be the first line
        filters.Add(new HandleErrorAttribute());
    }

You may also like

FluentSecurity + MvcSiteMapProvider = Better .Net Security Management

In my continual quest for find better ways of doing anything and everything this week I decided to tackle on of my arch nemesis: Security in ASP.Net, specifically Security in ASP.Net MVC3. You might ask why? We all know that there are at least 2 easy ways to implement security in MVC3.

Keep Reading

SharePoint Calendar view with date formatting

On one of our recent SharePoint implementations our client, like most, wanted to take advantage of the calendar functionality and list the next 5 events on the homepage. This was very simple to do but the issue we encountered was on the homepage. Whenever a full day event was displayed it was showing a start time of 12am.

Keep Reading

Newsletter sign up

Every couple of months we send out an update on what's been happening around our office and the web. Sign up and see what you think. And of course, we never spam.