Apr 022010

With the implementation of MVC there is a lot more room for unhanded global exceptions because in MVC you are encouraged to write more in line code to accompany the back end code. So what happens when an exception is thrown within the front end code? Well you’ll have an exception that will bubble up to the global application class which if you don’t handle it there it will continue to bubble and be displayed to the user as specified in the web configuration file. This doesn’t do you much good, sure the user will know that there is an error, and you can squelch the amount of information that is displayed to your ordinary users, but if working through trouble shooting the issue this basic information does you no good.

So where do you go from here? Well the first step is to decide on a method of storing your errors, this can be a database table or a file depending on your preferred method and environment. In the case of web applications that are database driven storing this error information in the database makes the most sense as it will put the information in a place that is easily query-able.

Next just as important as where to store the data is what data from the exception you want to store. My opinion is that the more information you store the better off you are, you never know when an error may be specific to a certain browser or something else quite random. So that’s why my preferred data structure for my error log table in my database is as follows:
Error Table Structure

All the fields contained within this table are of type Varchar besides the fields Err_ID which is an integer field and Err_Date which is a date time field. All fields need to be large enough to contain their respective data, in the case of SQL string and Stack Trace I would recommend varchar(max) to ensure you have enough room in the field to contain the appropriate data.

Now, once you have the data structure in place you need to add your code to the Global.asax.cs file in order to catch the exception. Below is an example of code that I have in place, in this instance I use LINQ to SQL and then redirect to my generic Error Page.

protected void Application_Error(object sender, EventArgs e)
    Exception Ex;
    for (Ex = Server.GetLastError(); 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();

    if (Context != null)

The specifics on what to do with your error once you catch it is entirely up to you, this is just the way that I use for my MVC applications that I create to track errors. It also may be beneficial if when you catch an error you send the Error details to yourself via email. This will allow you to be easily notified whenever an error occurs, the downside however is that it can quickly become overwhelming to your inbox if their is something that causes a multitude of errors. The upside of this though is it gives you more encouragement to resolve the error faster.

Now, this is by no means and end all to error handling in MVC, it’s should be accompanied by proper error handling in the code behind and of course use of proper logic to ensure reduction of errors.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.