views:

535

answers:

3

I would like to implement exception handling on a ASP.NET application. How do you suggest I implement it? Some requirements are:

  • User should see a friendly page when an exception occurs
  • Admin should get an email with exception details

I understand there are several ways of implementing exceptions (ex: Log messages in the event viewer)

What's the recommended approach?

+1  A: 

I wrote an article on this that I think you'd really like

http://dotnetblogger.com/post/2008/12/03/ASPNET-Health-Monitor.aspx

As for the friendly page bit, I just use the built in exception handling in the web.config... since you are implementing the unknown exception stuff in the global.asax

You can also handle exceptions in try/catch blocks and add a note to it if you want. Then just bind the database to a nice gridview for admins to easily see the errors.

rockinthesixstring
There are some comments on my article showing some other options (both built in and third party), but I still think that my approach is the most customizable. I now use it on all my projects.
rockinthesixstring
+1  A: 

The recommended approach really depends on what you need to do with the error information.

For simple apps, just setting up the web.config file to redirect to a friendly error page might be enough.

It's usually a good idea to record unhandled error information to a database, file, windows application log or web service by handling them with the Global.asax Application_Error event. You can also use the web.config to get your friendly page with that. Generally I wouldn't recommend using the windows error logs as they tend to be a bit obtuse, but there's nothing wrong with it.

For very detailed operational information, including warnings and info messages, tools like log4net are widely used. Usually you would go this route for products or enterprise level applications where your support teams need as much information as possible to diagnose the errors.

MSDN has a walkthrough of some of the basic error handling setups and what you can do with them.

womp
A: 

I have a nice reusable HttpModule that you can look at here. It can be used to easily deal with Exceptions that occur during normal user requests, and those that occur in background threads.

TheObjectGuy