ARTICLE

Logging Errors in a XML File

Posted by James Upton Articles | Exception Handling C# August 26, 2004
Error handling and logging errors at a common place is something, which is required in every piece of code you write. The attached source code is an error logger class that I've used in all of my applications. This class writes errors to XML files.
Reader Level:
Download Files:
 

Here is something I wrote for use in pretty much every application I use. It's an error logging class which logs all errors in a strongly typed XML file. It is version 1.0 as it were, and version 2.0 is currently in the pipe... It is fully commented (XML Comments), and is fairly self explanatory. It is of course written in C#.

This class is used for logging most types of System.Exception except for the following:

  • System.AppDomainUnloadedException
  • System.BadImageFormatException
  • System.CannotUnloadAppDomainException
  • System.ContextMarshalException
  • System.DuplicateWaitObjectException
  • System.EntryPointNotFound
  • System.MulticastNotSupportedException
  • System.ExecutionEngineException
  • System.ObjectDisposedException
  • System.PlatformNotSupportedException

Because I thought they were unlikely. Also includes SqlException(System.Data. SqlClient.- SqlException) which will insert as many errors as are necessary [it basically iterates the .errors collection]. Each call includes two free form strings of unlimited length (because it's a dataset) this allows you to add any two extra strings of whatever data to your error log - e.g. what userid caused the error, or what data they had submitted to the routine to cause the error - it's up to you, or leave them blank.

Does not deal with inner exceptions - wait until version 2..

COMMENT USING