23

I'm building an MVC 3.0 ecommerce site. I want to avoid sticking try-catch blocks all over my code.

Question is pretty simple - What is a good strategy for capturing all errors that are thrown on my website? I want to know about all of them... so I can work on bringing that count to 0 once the site goes live. I plan on writing each error to a logger and also to email it.

I've done some reading about capturing it in the global.asax file... but i've also read that it doesn't get ALL of them.

Anyone have any suggestions or can point me in a good direction?

Ralph N
  • 4,240
  • 8
  • 28
  • 36
  • 1
    I like all the answers... i tried the one about ELMAH and i'm loving it. Installed it so easy and i'm gonna replace it completely with my crappy logger. I'd like to see some votes/comments to make sure ELMAH is the way to go... – Ralph N Jan 03 '12 at 13:14
  • I agree! ELMAH is awesome and it is very easy to setup. I have used ELMAH in number of projects successfully. – azamsharp Jan 04 '12 at 04:15

2 Answers2

25

The MVC way of solving that problem are filters. If you are on MVC3 already, global filters are for you. There is special type of filter for handing errors: the HandleError filter. This article describes much of using it.

You might implement you own filter and control every aspect of of unhandled exceptions, e.g.:

public class HandleExceptionsAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        var expception = filterContext.Exception;

        // custom code here..
    }
}

Finally, if you want to have all unhandled exceptions to be stored and logged, ELMAH i.e. ELMAH.MVC is the best choice. Install it by using the NuGet package, configure it and all data will be accessed from a link such as:

http://yourapp/admin/elmah/

My suggestion is to not use anything like Application_Error in Global.asax.cs.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86
  • I went this route. Everything was pretty much set up for me using nuget, which is great. Only pitfall - people need to make sure they secure their elmah.axd file. Even when you put authorize attr on the elmah controller, you can still get to elmah.axd. If you block it, make sure you also can't do something like /a/b/c/d/elmah.axd... so it takes a little work. – Ralph N Jan 04 '12 at 12:42
  • Dont bother with elmah.axd, please even remove it from your web.config! Unfortunatelly I cannot do that automatically. Once Elmah.MVC is installed, you will have http://app/admin/elmah route registered. No problem to secure it by [Authorize] attribute; – Alexander Beletsky Jan 04 '12 at 17:10
  • some details here: http://www.beletsky.net/2011/03/integrating-elmah-to-aspnet-mvc-in.html – Alexander Beletsky Jan 04 '12 at 17:11
  • Hey Alexander, you are right. I commented out the httphandlers and handlers sections that had anything to do with elmah.axd in my web.config... and it turns out that we don't need it. The controller that was put into Area/Admin/Elmah seems to do everything without the elmah.axd stuff. I wont why the nuget elmah.axd just doesn't put that stuff in there (or rather, should put it in commented-out to begin with...) – Ralph N Jan 05 '12 at 11:33
  • 1
    Hey Alexander, one more comment. Your blog entry has all the information needed, but the comment at top in red is misleading: "UPDATE: Elmah.MVC is now released as NuGet package. No need to read that long blog post, just install it.". I assumed that your instructions were for people who were manual installing... but you do have this extra information about the handlers/httphandlers that is in fact a good reason to read the "long blog post" =) – Ralph N Jan 05 '12 at 11:37
  • Had to revisit this post one more time... I found that elmah logs when accessing in localhost, but stops logging when accessing from another computer (and the "custom error page" kicks in). See Ivan Zlatev's answer here: http://stackoverflow.com/questions/766610/how-to-get-elmah-to-work-with-asp-net-mvc-handleerror-attribute/779961#779961 – Ralph N Jan 05 '12 at 16:45
  • I know this is quite old, but I would like to know why using Application_Error is not recomended. – Javier Sep 08 '15 at 14:40
  • I've done both ... it seems that some things can only be caught by elmah and others live in the mvc domain, poor implementation imo from microsoft, it used to be that Application_Error caught everything but as time has gone on the goal posts have moved. – War Nov 27 '15 at 16:15
2

You can handle any thrown Exception in the Global.asax.

protected void Application_Error(object sender, EventArgs e) 
{
  Exception exception = Server.GetLastError();
  // global handling code goes here

  Server.ClearError();
}
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108