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. Power of the ASP.NET MVC + jquery
  3. ASP.NET selectors in jquery
  4. Modified properties in LINQ to SQL
  5. Linq – SubmitChanges does not work

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

  • Revium To Deliver The ACCC’s Energy Price Comparison website
  • Responsive Web Design
  • Vermont Consulting website launched
  • Code Compliance Monitoring Committee’s website launched
  • Revium: Victorian Government eService provider
  • BelcherPage2012 website goes for gold
  • Tangshan Burak’s website overhaul goes live.
  • Revium launch new Autism Victoria website, www.amaze.org.au

Twitter

  • Love Palidromes? Celebrating the last of the palidromic dates (dd/mm/yyyy format) for some time. http://t.co/PT5uuhel 1 day ago
  • http://t.co/x5TfWCGx http://t.co/1juE0ryg 1 week ago
  • Looking to keep your site working in the new browser versions? To know the current browser usage metrics, see http://t.co/GyffiOaZ 2011-11-30
  • 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.

13 Feb

Revium To Deliver The ACCC’s Energy Price Comparison website

The ACCC selects Melbourne Based Digital Agency Revium to deliver the Australian Energy Regulator’s Online Energy Price Comparison website. The major purpose of the energy price…

Continue reading
View archive

The blog.

07 Nov

Revium: Victorian Government eService provider

Revium is pleased to be accepted on the refreshed Victorian eServices Panel, and proud to be able to provide the Victorian Government with Systems…

Continue reading
View archive

The sandbox.

13 Feb

Responsive Web Design

From big to small In most recent times monitors have increased in size and 21 inch monitors and bigger are very common. This allowed web…

Continue reading
View archive

eNewsletter.

 

© Copyright 2012. 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