Plip's Weblog

Phil Winstanley - British .NET chap based in Lancashire. Enjoys tea and tech. Working for Microsoft.

Deployed an error free site? Are you Sure? Error reporting in ASP.NET 1.0 & ASP.NET 1.1

I have deployed more ASP.NET sites than I care to count. Many of these sites I was ultra confident they were error free as I'd covered every conceivable code path with error trapping and handling code. It was to my ultimate horror that when I added some simple "catch all" error handling to one of my sites, that the site was generating dozens of errors each day, and had been doing so since the site was launched months before.

I now make it standard practice that before any site that I or any of the development team I'm in creates, goes live, we add error reporting to the projects.

I have created a simple class (large - but simple) that sends the developers of a web site all the error information they could ever need to help diagnose errors that are occurring to their users.

The class has grown over about a year to it's current state, follows is a list of features: -

  • Error Stack dump, with line numbers, code file, error messages and inner exception walking.
  • Path details for the page that has thrown the error.
  • Path details for the referrer of the page that has thrown the error.
  • User's IP Address.
  • The Web Server Machine name that the error occurred on (Important in Web Farm scenarios).
  • Version of the .NET Framework the Application is running under.
  • Version of every assembly within the Application's Application Domain, including the time it was built (Time stamp only seems to work on W2K3 not W2K)
  • Full list of the content of the following collections
    • Querystring
    • Form
    • Cookies
    • Server Variables
  • Full list of the content of the following state bags, including supporting the IError interface defined below.
    • Session
    • Application
    • Cache
  • Trace Output (The output from ASP.NET that appears at the bottom of pages when trace="true" is specified in the Page directive, note this only shows when the trace property is set to true).
  • Error Flood protection, if your site is generating lots of errors, you don't want to be swamped with e-mails so this class has the ability to send you the same error X number of times in X number of minutes, then it stops sending for X minutes.

The Error reporting code is really easy to add to your site, if you don't want granular control, all you need to do is add an entry to your ASP.NET Global.asax's Application_OnError event as follows: -

        ErrorHandling.WebException WE = new ErrorHandling.WebException();
        WE.CurrentException = e;
        WE.MailFrom = ("mywebsite@mydomain.com");
        WE.MailTo = ("mydeveloper@mydomain.com");
        WE.Site = ("My Web Site");
        WE.FloodCount = 20;
        WE.FloodMins = 5;
        WE.Handle();
        

What the above does is send any errors to mydeveloper@mydomain.com up to a total of 20 times for each unique stack trace, over a period of 5 minutes.

There is also an interface on the Class which allows you to have any of your own objects within the State bags (Session, Application and Cache) output the custom debug information you wire in to them.

But to get to the stage of sending the errors being created in your site, you need to actually get hold of the class files, I've included them with a sample Global.asax zipped up so you can download them here.

There is only one more thing to note about this class, it has protection for "Application is Restarting" errors, we had a Web Server go loopy one day, and it sent us so many errors it killed our connection back in the office, meaning we could no longer fix the problem, the class will no longer send "Application is Restarting" errors.

Posted: Apr 11 2004, 02:02 AM by Plip | with 16 comment(s)
Filed under:

Comments

Anatoly Lubarsky said:

It is very old stuff. People use it from the early .net days.
# April 11, 2004 11:33 AM

Phil Winstanley said:

Yup it is, but I can't underestimate how important it is, there are new developers coming online all the time, I started off with an ASP 3.0 version way back when I began programming - it's been an invaluable learning tool for me, discovering all the niggly little errors that happen when gremlin Proxy server get in the way or when Monsters eat the view state.
# April 11, 2004 11:44 AM

webber said:

you really should add a readme file to the download so peopole can know what to do with all of these files.

don't assume everybody is an expert
# April 16, 2004 4:19 AM

webber said:

you really should add a readme file to the download so people can know what to do with all of these files.

don't assume everybody is an expert
# April 16, 2004 4:20 AM

Phil Winstanley said:

Thanks for the suggestion, I'll take you up on that and make sure a ReadMe goes in with the next version.

Which is coming soon.
# April 16, 2004 4:21 AM

Margo said:

I feel like a newbie now. What you've written looks very interesting but what do I do with the files?
# May 5, 2004 6:51 AM