Development – Leet 4 Life

Software Development Code Samples and More

Skip to: Content | Sidebar | Footer

Global Exception Handling and Logging in ASP.NET or MVC

19 April, 2012 | ASP.NET, C#. NET | By: Michael Merrell

Global Error Handling and Logging is something that I feel no production ready application should be without. The last thing you want your end users to see is a server exception page when something drastically goes wrong. As with most coding practices this shouldn’t be a replacement for localized error handling but more of an addition to ensure that the end user’s experience is the best that it can be.

To add Global Error handling and logging you need to go into the Global.asax.cs file in the root of your ASP.NET or MVC Application. If the Global.asax file does not exist you will need to create it. Inside of that file you will then proceed to modify or create the Application_Error method. Inside of the method you can add whatever code you want, below you will find an example of something I’ve written previously for logging errors.

protected void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    if ((ex is HttpException && ((HttpException)ex).GetHttpCode() == 404) && Context != null)
    {
        Context.Response.ClearHeaders();
        Context.Response.StatusCode = 404;
        Context.Server.Transfer("~/404.aspx");
        return;
    }

    for (var Ex = ex; Ex != null; Ex = Ex.InnerException)
    {
        DataContext DB = new DataContext();
        Error_Log Err = new Error_Log();

        Err.Err_Date = DateTime.Now;
        Err.Message = Ex.Message;
        Err.Stack_Trace = Ex.StackTrace;
        Err.Source = Ex.Source;
        Err.Method_Name = Ex.TargetSite.Name;

        if (Context != null)
        {
            Err.IP = Context.Request.UserHostAddress;
            Err.Class_Name = Context.CurrentHandler.GetType().ToString();
            Err.BrowserAgent = Context.Request.Browser.Type.ToString() + " | " + Context.Request.UserAgent;
            Err.URL = Context.Request.Url.ToString();
            if (Context.Request.UrlReferrer != null)
            {
                Err.ReferringURL = Context.Request.UrlReferrer.ToString();
            }
        }

        DB.Error_Logs.Attach(Err);
        DB.SubmitChanges();
    }

    if (Context != null)
        Context.Response.Redirect("~/Error.aspx");
}

You can see my original implementation and data model for the class I use here.

Inside of this class you can log and report the exception however you want. In this instance each of the inner exceptions as well as the main exception are logged individually. My more recent implementations I have consolidated it so that there is only one entity created for the entire exception stack. Also I have implemented logic that will email details about the exception and the user to a pre-defined email address.

Those are just a couple of examples, you can also redirect based on current page or error type and add in a lot of different logic to make error handling useful for you as the developer and pleasant for your end users.

Write a comment