Yesterday I spent a really exciting day coming up with error trapping routines on one of my sites. I find this part of programming very dull and I know if I did it while I was writing the pages then it would save a lot of time. However for my faults I always seem to end up leaving these things until a later date.
The reason I was suddenly very interested in getting it sorted was that the site in question has some 4000 plus pages and it’s very hard to make sure all those pages are doing as they should. From some of the results I’d seen in the Google webmasters tools I suspected at lease some of my pages were throwing up errors but it was very difficult to track down what and where exactly.
I decided that the best thing I could do is to take the sledgehammer approach and get notification myself for every single error as it comes up. So with this in mind I created a generic error routine in my sites Business Logic Layer as such:
Public Overloads Sub SendErrorEmail(
ByVal strURL As String, _
ByVal strError As String, _
ByVal Request As HttpRequest)
Dim objEmail As New EmailBLL
Dim message As String = "<font face=verdana color=red>" _
& "<h4>" & strURL & "</h4>" _
& "<pre><font color='red'>" & strError & "</pre>" _
& "</font>"
If Not Request.UrlReferrer Is Nothing Then
message &= ("<pre>Referer:" & Request.UrlReferrer.ToString()_
& "</pre>")
Else
message &= ("<pre>Referer: no referer</pre>")
End If
message &= ("<pre>User Agent: " & Request.UserAgent() & "</pre>")
message &= ("<pre>Host Address: " & Request.UserHostAddress.ToString & _
"</pre>")
message &= ("<pre>Host Name: y" & Request.UserHostName & "</pre>")
objEmail.SendHTMLEmail("system@website.com", "admin@website.com", _
"Content Error", message)
End Sub
I can’t claim credit for all of this code as some of it I did use from another resource. I did expand upon it though to include the “Request” object. This means that I can see where the error originated, either from an end user or a search spider and the browser version etc.
The “overloads” is there as I have two versions of the code one with and one without the “Request” object. Also the last line of the code is simply a call to another custom routine of mine that sends an HTML email.
So now that I had my error email routine I just needed to trigger it somehow and this is where I come to the sledgehammer approach. On every single one of my pages I put the following code in the Page_Error event of the code behind page:
Protected Sub Page_Error(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Error
Dim objErrorBLL As New ErrorTrappingBLL
objErrorBLL.SendErrorEmail(Request.Url.ToString(), _
Server.GetLastError().ToString(), Request)
End Sub
So basically this will send me an email whenever any error appears on any page of the site. Initially I was inundated with error emails, but it was worthwile as I’ve now gone into every one of those pages and fixed them and now the emails are down to a slow trickle of just one or two a day.
Of course what this doesn’t do is tell me of any problems on a page that are not causing error messages such as broken affiliat links etc but it was still a very worthwile exercise and if your worried about your .Net site I recommend you do the same.

March 9th, 2007 at 4:42 am
[...] About two weeks back I wrote about an error reporting and handling routine that I added to every page of my digital lard website to try and capture fix some of the many page errors I was getting. Here is a link to the article for reference. [...]