I woul开发者_如何学编程d 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?
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.
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.
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.
精彩评论