Is there a centralized error handling process in C #

Is there a way to centralize error handling or exception handling without using try catch methods?

+3
source share
4 answers

If this is for ASP.NET, you can add the file Global.asaxto the site and process the method Application_Error.

This is how I usually use it:

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs
    if (!System.Diagnostics.EventLog.SourceExists("MySource"))
    {
        System.Diagnostics.EventLog.CreateEventSource("MySource",
            "Application");
    }
    System.Diagnostics.EventLog.WriteEntry("MySource",
        Server.GetLastError().ToString());
}
+2
source

Use the AppDomain UnhandledExceptionevent:

    static void Main(string[] args)
    {

        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

    }

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        // log the exception 
    }

To use ASP.NET you will use glabal.asax.

+8
source

If you use WinForms, you can see my other answer related to this . He uses try-catch, though, as there is no other way that I know.

See other answers for ASP.NET and other possible .NET applications.

0
source

Source: https://habr.com/ru/post/1764478/


All Articles