Thursday, 25 August 2011

How you can log errors Part 1

In this post, which is in two parts, i would like to discuss about ways you can handle and log errors. It is really important to think about the strategy you are going to use to handle errors at an early stage. Why? Let us take a look at a simple example:


This is a simple try catch statement whereby the error is shown in a message box. If the application start to grow and there has't been a proper infrastucture layed out on how to cater for errors, there is going to be a lot of try catch statements as the one above, hence there will be a lot of duplication. You are going to need help with the project, and you are going to employ other developers to help. There are higher chances that the developers are going to employ their own ways of handling the errors and hence there is going to be inconsistency as far as handling errors is concerned.

From the above example, there is an important piece of information that is missing, the stack trace. It is not advisable to show the whole error to the user, but there should be a way of logging the error details so that it will be easy to trace the error, maybe to log to a file or send an email. Hence if you don't plan ahead there can be information loss concerning bugs that can occurr within the application. It is, therefore, important to plan on how you are going to handle errors at an early stage of development due to the facts i mentioned and others that you might have come across.

How can i handle errors?

There are different ways you can handle errors depending with the development environment you are in. But as you plan on how you are going to do it, one thing you must bear in mind is that it must be easy for maintenance, to use and to configure. Here are some of the tools you can use depending with your needs:


  • Trace.axd
  • Log4net - logging.apache.org/log4net/index.html
  • ELMAH - code.google.com/p/elmah/
  • PostSharp - www.postsharp.org
Apart, from these tools, you can create your own custom error handling tool or library. I personally have used Log4net. It is easy to configure and there is support for it from the community, though it is now looking like there aren't any upgrades being done. However, despite that fact, you have a wide range of tools to choose from that you can use to handle you errors.

Therefore, as you plan your architecture, you might want to consider which tool you can use that can complement with what you want to achieve. For instance, apart from, showing friendly errors to users, you might want to receive emails concerning critical operations that might have failed to execute, and that they would need your immediate attention. In this case, i would go for Log4net as an example.

If you have used any one of the tools that i have mentioned, i would like to hear about your experience. Or if you have used another tool apart from these ones, please do share. In the next part, i will go ahead and show you how you can address the problems that i mentioned earlier on if you don't plan on handling errors.



























No comments:

Post a Comment