Creating Greater Application Error Transparency

Application Error Tracking

Often times, when we develop applications for the real world we are put in a position where we can’t just test our applications in a normal debug fashion. Perhaps the error is only caused on a production server while the local or test servers work fine. Also, applications in a production environment tend to work with several other external applications, services, resources, etc. that are not available or apparent on a local machine.

By adding a little code to the global.asax file which is triggered anytime an application breaks, we can peer into problems we experience; and even become proactive in addressing issues before our customers complain.

This advanced warning, can really open your eyes to just how often a web application is crashing without you even knowing it, as well as provide a wealth of information for hard to replicate issues such as bugs from specific browsers, sessions, cookies, or even individual users!

Global.asax VB Code

' Code that runs when an unhandled error occurs
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    'Get reference to context
    Dim Context = HttpContext.Current
    
    'Create new exception
    Dim Exception As New Exception
    
    'If non-local, then send error (while debugging/developing, etc. doesn't constantly throw errors)
    If Context.Request.Url.AbsoluteUri.IndexOf("localhost") < 0 Then
        'NOTE: If you have session values that are pertinent to identifying user, session, interaction, etc.
        'this is a good place to reference those for display as well.
        
        Try
            'Get reference to thrown error
            Exception = Context.Server.GetLastError()
            
            Dim sb As New StringBuilder()
            
            sb.Append("An error has occurred on " & ConfigurationManager.AppSettings("baseHref") & ". A user requested " & Request.Path.ToString & chr(13) & chr(13))
            sb.Append("OS: " & Request.Browser.Platform & chr(13))
            sb.Append("Machine Name: " & Environment.MachineName.ToString & chr(13))
            sb.Append("Browser: " & Request.Browser.Browser.ToString & " - Ver. " & Request.Browser.MajorVersion.ToString & chr(13))
            sb.Append("Method Name: " & Session("METHOD_NAME") & chr(13))
            sb.Append("Source: " & Exception.Source & chr(13))
            sb.Append("Message: " & Exception.Message & chr(13))
            sb.Append("Message Details: " & Exception.InnerException.ToString() & chr(13))
            sb.Append("Target Site: " & Exception.TargetSite.Name & chr(13) & chr(13))
            sb.Append("Stack Trace: " & Exception.StackTrace & chr(13))
                        
            'NOTE: Add delivery mechanism (email, write to log file, write to db, write to event viewer, etc.) for sb.toString()
            
        Catch ex As Exception
            'Custom handling here... perhaps no logging done, retry, or try tiered logging if one log mechanism fails...
            
            Exit Sub
        End Try
    End If
End Sub

Special thanks to Budi Awan, who first brought this up to me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s