locked
Centralized error handler object? RRS feed

  • Question

  • User-317007944 posted

    I used Delphi language a few years ago and there was a centralized error handler object where any error caused within any method of my system was triggered to this "centralized object" and I could for example write the error to my database, redirect the user to an error page, login page, etc.

    Is there something like this inside Visual Studio or is it possible to create a class like this where it could be triggered if any method of my system gets an error, or do I really need to repeatedly create the clauses: try, catch, finally inside each method of my system individually?

    try {
    
        <code>
    
    }
    catch (Exception ex) {
        ErrorLog.Error(ex);
    }

    What is the best approach in this case?

    Thursday, January 16, 2020 3:32 PM

Answers

  • User475983607 posted

    Depends on the framework.  For ASP.NET, unhandled exceptions bubble up to Application_Error in the Global.asax.   You can write code to log exception in this event.

    https://odetocode.com/articles/69.aspx

    This pattern works for a common logger.

    public class ErrorLog 
    {
    	public static Error(Exception ex) 
    	{
    		//Call the data access layer
    	}
    }

    Exception in ASP.NET Core are handled in middleware.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 16, 2020 3:55 PM
  • User533502567 posted

    Is there something like this inside Visual Studio or is it possible to create a class like this where it could be triggered if any method of my system gets an error, or do I really need to repeatedly create the clauses: try, catch, finally inside each method of my system individually?

    You can add error handling at the application, page, and code levels. Check this link for few examples:

    Application-Level Error Handling:  you can use Application_Error event in Global.asax to catch unhanded exceptions..

    // IN Global.asax
    public class MyApplication : System.Web.HttpApplication
    {
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception unException = Server.GetLastError();
            if (unException != null)
            {
                if (exc is HttpUnhandledException)
                 {
                     // Pass the error on to the error page.
                       Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
                 }
                //log error to DB ??
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 16, 2020 4:02 PM
  • User1120430333 posted

    The link gives all the ways global exception handling can be implemented based on project type

    https://stackify.com/csharp-catch-all-exceptions/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 17, 2020 3:49 PM

All replies

  • User475983607 posted

    Depends on the framework.  For ASP.NET, unhandled exceptions bubble up to Application_Error in the Global.asax.   You can write code to log exception in this event.

    https://odetocode.com/articles/69.aspx

    This pattern works for a common logger.

    public class ErrorLog 
    {
    	public static Error(Exception ex) 
    	{
    		//Call the data access layer
    	}
    }

    Exception in ASP.NET Core are handled in middleware.

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/error-handling?view=aspnetcore-3.1

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 16, 2020 3:55 PM
  • User533502567 posted

    Is there something like this inside Visual Studio or is it possible to create a class like this where it could be triggered if any method of my system gets an error, or do I really need to repeatedly create the clauses: try, catch, finally inside each method of my system individually?

    You can add error handling at the application, page, and code levels. Check this link for few examples:

    Application-Level Error Handling:  you can use Application_Error event in Global.asax to catch unhanded exceptions..

    // IN Global.asax
    public class MyApplication : System.Web.HttpApplication
    {
        protected void Application_Error(object sender, EventArgs e)
        {
            Exception unException = Server.GetLastError();
            if (unException != null)
            {
                if (exc is HttpUnhandledException)
                 {
                     // Pass the error on to the error page.
                       Server.Transfer("ErrorPage.aspx?handler=Application_Error%20-%20Global.asax", true);
                 }
                //log error to DB ??
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 16, 2020 4:02 PM
  • User1120430333 posted

    The link gives all the ways global exception handling can be implemented based on project type

    https://stackify.com/csharp-catch-all-exceptions/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 17, 2020 3:49 PM
  • User-317007944 posted

    Great!

    Friday, January 17, 2020 10:48 PM
  • User-317007944 posted

    Thanks!

    Friday, January 17, 2020 10:49 PM