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. Power of the ASP.NET MVC + jquery
  4. ASP.NET selectors in jquery
  5. Modified properties in LINQ to SQL

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

  • FluentSecurity + MvcSiteMapProvider = Better .Net Security Management
  • 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.

Twitter

  • Sweet ... SugarCRM has new v6.5 for easier nav, rapid access. Call us if you need to get the best out of your CRM http://t.co/iqAcNO6F 6 days ago
  • Using Social Logins? - Nice article on trends and pitch for an aggregrated sharing approach for your site. See http://t.co/ZxFJqyAW 1 week ago
  • What are the issues and pitfalls fir choosing a CMS for your website. Come to the Internet Show in Melbourne on... http://t.co/bojO7dVV 2 weeks ago
  • 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.

08 May

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:…

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