Error Handling

As I see it, error handling can be as complex as the main application itself. The reason that the solution to error handling is so complex is because one size never fits all. I would quickly question the credentials of anyone in IT that has the “one size fits all” mentality to any aspect of development. One size can never fit all due to the huge variety of development environments that exist in the IT industry. Some developers work in teams as small as 10 people, and other teams wish they had as many as 10 people! Depending on your background, you may favour some approaches and ignore others.
Before I go into the details, I would like to point out that I use the term error handling to refer to both errors and exceptions only as a matter of preference.

Different ways to log errors

There are many different ways to log errors and alert administration of them. The main logging methods are described below in detail.
• Database Logging
• Emails
• Text Messages
• Text Files
• Windows Event Viewer

Database Logging

Databases are very suitable for error logging. Errors may be stored quite simply in the database within one database table as shown below:
Table name: ErrorLog
ErrorID ErrorDescription StackTrace IssueDate
101 File Not Found The file ''''/sdf.aspx'''' does not exist. 2006-08-01 20:30:32
102 Product Search failed Object reference not set to an instance… 2006-08-01 20:34:09
103 Product Search failed Object reference not set to an instance… 2006-08-01 20:41:32

The ErrorDescription field is a custom description. The text I have put in the StackTrace column consists of shortened versions of 2 messages that are all too familiar to developers. It is always a good idea to log errors in a different database than the one being used for the application. Otherwise, if the database goes down,
how would you log this using the database method?
The ErrorID column is an Identity column to uniquely identify each record in the table. The StackTrace column in this example is intended to hold the contents of the StackTrace property of the exception. Obviously, its entire contents cannot be fit in the preview here so I have only included the relevant part within each row.

As you can see from this example, storing the contents of the stack trace for each error may seem like overkill. The Custom Summary Report example given later uses the database logging method as part of a more sophisticated logging strategy. I am therefore saving the detailed examination of using databases until later. You may find that the Custom Summary Report example is too much for your resources and timescales and perhaps the email or text message techniques given below will suffice.


Some web applications send out an email every time an error occurs. I see such examples many times when I glance over ASP.NET textbooks. I personally think sending out an email per error is overkill. I have seen this done within commercial applications, usually when the website originally serviced a small number of people, but then when the company expands and more people use the site the errors become more frequent and what was manageable is now just too much to handle. It is just like the overloading mail server or simply give the administrators too much to look at.

Text Messages

Mobile phone text messages can be a good choice when the administrators of the application are off-site and there is a problem. How many people these days go somewhere without their mobile phone? Emails can be as descriptive as necessary and it was previously outlined that you should take full advantage of that. However, text messages should be kept short. But again this kind of error logging will kill the mail server at some time.

Text Files

Text files are a very basic way of logging errors, but are useful when other mediums fail, provided that these files are maintained and not simply allowed to keep growing and consuming more and more space, which can cause other problems. A problem with the database may stop the errors being logged if database logging is used and the same database is being used to log errors, although this is not recommended. However, this problem can still happen when a separate database is used for error logging. When the database logging fails, you could use text files as a simple secondary medium.

Windows Event Viewer

This is another popular technique and is actually used by many applications already running on Windows machines. However, it does require that the administrators of the application have access to the production server. If it is a large organization and the server is in a different state, this can be a problem when there are networking issues.

SQL Server, firewall programs, anti-virus programs and Windows itself report information in the Event Log, including errors. I cover it here only because I know it to be used for error logging.

I suppose the Event Log would be suitable if you built an application that acted as a report interface to the Windows Event Log. The error logging puts the data into the seemingly difficult-to-read Event Log and then the interface application derives reports and statistics from it. I do however believe the other methods and techniques outlined within this article to be better, quicker to implement and more effective in most cases. Another reason for choosing the Event Log as an option is if you have no time to implement the other options. It is only a 1 line call within the code when you are using the Microsoft Application Blocks. Perhaps you can do this while you implement a better way for the next deployment.

Factors – that might affect your error handling option to choose

  • Company Size
  • Company Culture
  • Deadlines versus Quality

~ by UTS on May 29, 2009.

Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: