views:

521

answers:

5

Hi All

Is there an easy way to log all exceptions in an ASP.NET application? I'm already logging unhandled exceptions through the Application_OnError event, but I want to perform logging even when an exception is handled on a page level.

Many thanks.

+1  A: 

Call your logging component in the catch blocks for exceptions that are handled.

Colin Mackay
Well, yes. But I'm wondering if there is not an easier way to do it? Without modifying each catch statement individually?
Ravish
Why do you have catch statements if you don't handle the error?If you handle the error then there is no need to clog your log with it?!
Richard L
I do handle the errors for the sake of the user, but I need more information for debugging purposes than what is displayed to the user.
Ravish
A: 

I don't think there is anyway of automatically logging handled exceptions. If you're already doing the Application_OnError logging for unhandled exceptions I'm afraid that Colin's correct and you're going to have to call your logging component in the catch blocks for the handled exceptions you want logging for.

Andrew Barrett
+3  A: 

Assuming you are only throwing and catching your own exception-types (derived from System.Exception ofc), you could make your base-exception log whatever you need in its constructor.

cwap
that's a good idea
Andrew Barrett
This is unlikely, as many exceptions will be thrown by standard .Net components - rethrowing these as your own would be very wasteful
ck
Hmmm, interesting angle. I would have to provide my own base exception type and then modify each catch statement on it's own anyway, but then at least all my logging functionality is in one place.
Ravish
@ck - Do you mean exceptions thrown by, for example, server controls, i.e. a dropdownlist being set to an invalid selectedindex?
Ravish
Taking this is as the most acceptable solution, as the only other ones were "can't be done". Thanks Meeh.
Ravish
In a real life application you can't be catching only your own exception-types. What you can do is catch them in uppper layers (i.e. UI). This would required you to wrap every caught exception in your own exception-type at lower layers and re-throw them. However this still does not cover all scenarios.
Piotr Owsiak
+2  A: 

You could use Elmah to log your exceptions. It's really easy to use and gives good information about the problem.

Richard L
"you get the following facilities without changing a single line of your code: * Logging of nearly all unhandled exceptions. "
Ravish
+20 For recommending Elmah. It is has become a standard when developing IMHO. If I recall SO also uses it.
Diago
A: 

ASP.NET Health Monitoring does this in an elegant and fairly automatic way (no need to manually catch exceptions):

When you click the first link, there is an example of how to enable and configure ASP.NET Health Monitoring. You need to configure it in web.config file.

For logging to a DB, you can create the corresponsing table using the ASP.NET SQL Server Registration Tool: ASP.NET SQL Server Registration Tool (Aspnet_regsql.exe)

I really like it. For manual logging, patterns & practices Enterprise Library's Logging Application Block might be something for you, but this is slightly off-topic.

Matthias

Mudu