One thing I really like about .NET is the flexibility and speed it gives you to implement new functionality. By using ASP.NET's Global.asax file, you can very easily hook into any major application events that occur in your system. Perhaps one of the most useful of these is the Application_Error event, which fires everytime an unhandled exception occurs within your site. During your testing phase, this can be a very useful place to find out how your users are experiencing your stystem.
One of the projects I'm working on has just gone back to the stakeholders and users, who are now trying out the functionality to see if there's anything they've left out. With the test team working off the same copy, there's a good number of people accessing the system. When stuff breaks, it's critical you find out why. Some users are good and will cut & paste you the exception + stacktrace, some will go better and add the URL or tell you the page they were on, and I'm still waiting on those who will go through a step-by-step process to recreate the error - but you can always wish.
Instead of relying on the user to remember to log the bug or tell the dev team about such blaring issues, I figured it would be more reliable to get the system to log its own bugs. Here's a way to edit your Global.asax so that it will email you back such exceptions:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError().GetBaseException();
string user = HttpContext.Current.User.Identity.Name;
string url = HttpContext.Current.Request.Url.ToString();
//Emails the exception to me
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(
"projectXxx@Yyy.com",
"adenhertog",
"BUG: PROJECT- " + ex.Message,
string.Format("For user: {0}\nAt: {1}\n{2}", user, url, ex.StackTrace));
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.server");
client.Send(mail);
}
This is Ok for small projects, but more often or not you'll be using a bug tracking system of some sort. We run Bugzilla where I am, which exposes web services (XMLRPC) that ultimately can be used in conjunction with the Global.asax code to get the system to log its own bugs.
If your bug tracking system supports web services, or even if it runs off a database, by modifying the Application_Error code, you too can ensure that bugs don't go unreported.
posted @ Monday, November 26, 2007 10:40 AM