ASP.NET Website Error Management
When I was a child growing up in rural Wisconsin, there was a popular term used when you saw an ant in the house, “For every one you see there is 100 you don’t see”. This was the tactful way to tell someone that killing that one ant you see isn’t fixing the problem and that they can’t just ignore the problem and hope it goes away (or just kill the ants when you happen to see them).
This is similar to someone alerting you to an error with a web page. Except that it’s probably more like 100,000 page errors before someone contacts you to tell you about the problem. The point is that you shouldn’t wait for someone to tell you there is a problem with your website; instead you need to actively monitor your pages to make sure things are running well. There are many services available to monitor your website for you such as Web Page Load Time Tracker but step number one is good back end error control.
With my projects I make sure that any back end errors generate an email. This notifies me and forces me to deal with the issue. It also gives me an idea of how prominent the error is based on how many emails I receive with the same error. To accomplish this with ASP.NET I put together a nifty little error reporting class:
Imports Microsoft.VisualBasic
Public Class ErrorReporting
Inherits System.Web.UI.Page
Protected Overrides Sub OnError(ByVal e As EventArgs)
Dim ctx As HttpContext = HttpContext.Current
Dim exception As Exception = ctx.Server.GetLastError()
Dim theSiteID As String = Session("SiteID")
Dim theBrowser As String = ctx.Request.Browser.Browser
Dim theQueryString As String = ctx.Request.QueryString.ToString
Dim theRawURL As String = ctx.Request.RawUrl.ToString
Dim theURL As String = ctx.Request.Url.ToString
Dim theReferrer As String = ""
Dim theHostAddress As String = ctx.Request.UserHostAddress
Dim theMessage As String = exception.Message
Dim theSource As String = exception.Source
Dim theStackTrace As String = exception.StackTrace
Dim theShortError As String = exception.Message.Substring(0, exception.Message.IndexOf("."))
Dim theSession As String = Session("SessionID")
Dim errorInfo As String = ""
errorInfo = errorInfo & "Site: " & theSiteID
errorInfo = errorInfo & "Browser: " & theBrowser
errorInfo = errorInfo & "Query String: " & theQueryString
errorInfo = errorInfo & "RAW URL: " & theRawURL
errorInfo = errorInfo & "URL: " & theURL
errorInfo = errorInfo & "User Host Address: " & theHostAddress
errorInfo = errorInfo & "Exception Message: " & theMessage
errorInfo = errorInfo & "Exception Source: " & theSource
errorInfo = errorInfo & "Exception Stack Trace: " & theStackTrace
errorInfo = errorInfo & "SessionID: " & theSession
Dim theSendClass As New SendEmail
theSendClass.SendMail("myEmailAddress ", "MyEmailPassword", "Website Error: " & theShortError, errorInfo)
Response.Redirect("http://" & cf.getSiteFQDN(Session("SiteID")) & "/GeneralError.aspx")
MyBase.OnError(e)
End Sub
End Class
Then in my web pages I change the default “Inherits System.Web.UI.Page” To “Inherits ErrorReporting”
By doing this, every time there is an uncontrolled back end website error, the class collects the exception information, current session information, URL and browser type, then sends me an email with all the details, then redirects the user to a friendly error page.