6

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
  • Do you mean handled by catching it, or using Page_Error, or something else? Please be more specific.
    – RichardOD
    Jul 15, 2009 at 7:44

7 Answers 7

9

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

2
  • "you get the following facilities without changing a single line of your code: * Logging of nearly all unhandled exceptions. "
    – staterium
    Jul 15, 2009 at 7:50
  • 1
    +20 For recommending Elmah. It is has become a standard when developing IMHO. If I recall SO also uses it. Jul 15, 2009 at 7:53
5

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.

5
  • 2
    This is unlikely, as many exceptions will be thrown by standard .Net components - rethrowing these as your own would be very wasteful
    – cjk
    Jul 15, 2009 at 7:37
  • 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.
    – staterium
    Jul 15, 2009 at 7:38
  • @ck - Do you mean exceptions thrown by, for example, server controls, i.e. a dropdownlist being set to an invalid selectedindex?
    – staterium
    Jul 15, 2009 at 7:41
  • Taking this is as the most acceptable solution, as the only other ones were "can't be done". Thanks Meeh.
    – staterium
    Jul 15, 2009 at 9:58
  • 2
    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. Jun 14, 2010 at 12:25
3

I agree with Collin, that you should log wherever you catch (and don't rethrow).

But if you need that every exception logged without much code modifications, then handle the AppDomain.FirstChanceException Event, and you'll get ALL the exceptions (maybe that's even too much :p, but it could help you clean the code).

1

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

3
  • Well, yes. But I'm wondering if there is not an easier way to do it? Without modifying each catch statement individually?
    – staterium
    Jul 15, 2009 at 7:35
  • 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
    Jul 15, 2009 at 7:41
  • 2
    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.
    – staterium
    Jul 15, 2009 at 7:43
1

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

0

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.

0

http://www.asp.net/hosting/tutorials/logging-error-details-with-asp-net-health-monitoring-cs

1
  • 2
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – GDP
    Aug 7, 2012 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.