ASP.NET Exception Handling/Logging

后端 未结 7 854
感动是毒
感动是毒 2020-12-20 20:31

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

相关标签:
7条回答
  • 2020-12-20 20:45

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

    0 讨论(0)
  • 2020-12-20 20:46

    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 讨论(0)
  • 2020-12-20 20:49

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

    0 讨论(0)
  • 2020-12-20 20:50

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

    • ASP.NET Health Monitoring Overview
    • How To: Use Health Monitoring in ASP.NET 2.0

    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 讨论(0)
  • 2020-12-20 20:56

    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).

    0 讨论(0)
  • 2020-12-20 20:59

    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.

    0 讨论(0)
提交回复
热议问题