ASP.NET MVC3 – Application_Error not firing log4net

by Mark Wiseman on September 26th, 2011 | Posted in Revium Sandbox | Read the comments

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 we are seeing the error emails / log we were 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 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 dosen’t Application_Error fire?

The best explanation if 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());
}

Related posts:

  1. ASP.NET MVC [HandleError] and logging
  2. FluentSecurity + MvcSiteMapProvider = Better .Net Security Management
  3. Creating a generic settings repository in C#
  4. Power of the ASP.NET MVC + jquery
  5. Kentico 6.0 Custom BizForm Validation

Tags: ASP.NET MVC, c#, log4net

« SharePoint Calendar view with date formatting

Responsive Web Design »

2 Responses to “ASP.NET MVC3 – Application_Error not firing log4net”

  1. Scott says:
    February 19, 2012 at 12:18 pm

    In the example code, the call to PutError is passing two arguments, the message and Exception, but your implementation of PutError listed only takes a single argument, the message.

  2. Mark Wiseman says:
    February 20, 2012 at 8:32 am

    Thanks @Scott. I have fixed it now

Leave a Reply

Click here to cancel reply.

Recent Articles

  • ISAF Sailing World Cup
  • Revium Supports the Prostate Cancer Foundation of Australia
  • Kentico FAQ Module
  • Advanced Visitor Tracking in Analytics
  • Kentico, Smart Search and filtering attachments
  • Enhancing JIRA’s Issue Navigator
  • Mobile Browsing
  • Revium help win gold for Australia

Twitter

  • All things Gold! http://t.co/9DkkjmAr 2012-09-13
  • Mat Belcher - our favourite London Gold Medalist dropped into the office to say thank you. http://t.co/TxHbe2y6 2012-09-13
  • You beauty - http://t.co/1kbcBZwg #london2012 @belcherpage2012 2012-08-10
  • More updates...

Revium Logo

  • Home
  • About
  • Expertise
  • Showcase
  • Contact

  • news
  • blog
  • sandbox
  • twitter
  • rss
  • visit our facebook page

We are Revium, hear us roar!

The news.

26 Feb

Revium Supports the Prostate Cancer Foundation of Australia

We here at Revium are proud to say that we are supporting the Prostate Cancer Foundation of Australia, this month we have been able…

Continue reading
View archive

The blog.

11 Apr

ISAF Sailing World Cup

Mat Belcher and his team have been successful in taking out round 3 of the ISAF Sailing World Cup in Palma de Mallorca a…

Continue reading
View archive

The sandbox.

20 Dec

Kentico, Smart Search and filtering attachments

We had a scenario recently where we had 2 indexes set up in Kentico to search different folders within our site. Everything worked as…

Continue reading
View archive

eNewsletter.

 

© Copyright 2013. All Rights Reserved.

Revium Pty Ltd

info@revium.com.au Work +61 3 9429 2000

10 Harvey Street
Richmond, Victoria, 3121 Australia
View map

Disclaimer and privacy Revium Pty Ltd

Find us: web development, seo

 
Partner logos