This article explains how to log errors (actually exceptions) which have occured in your app into an XML file. My error log is actually simple class
ErrorLog in namespace
DNH.Common.Errors. It's part of my library
DNH.Common and I use this class in my projects. Although it is very simple, I think that functionality of
ErrorLog really helps in debugging applications. It shows how to write XML using
This article provides some basics of:
- writing XML files using
- handling exceptions using
- detecting unhandled exceptions using
Using the code
First at all, you must add reference to assembly dnh.common.errors.dll into your project. Then you must write
using DNH.Common.Errors or use full name of class:
DNH.Common.Errors.ErrorLog. Then simply declare variable of
ErrorLog class type. Calling constructor is simple:
m_log = new ErrorLog(this.Server.MapPath("errorlogs/SampleXMLLog.xml"))
Only argument passed is path to XML file. First time this is called,
will create new file. So be sure it has read/write access to that virtual directory.
As I wrote,
ErrorLog writes down errors when Exception is thrown. Exception must be catched in
try-catch block. Or in ASP.NET web apps, it can be also obtained by calling
Server.GetLastError() (probably in Global.asax.cs,
This is handler in Global.aspx.cs:
protected void Application_Error(Object sender, EventArgs e)
Points of Interest
This code writes data from
Exception into XML writer:
private void WriteException(Exception e, XmlWriter xmlWriter)
This method write data into XML file using method above:
public void SaveExeptionToLog(System.Exception e)
string strOriginal = null;
XmlTextReader xmlReader = new XmlTextReader(m_logFileName);
xmlReader.WhitespaceHandling = WhitespaceHandling.None;
strOriginal = xmlReader.ReadInnerXml();
XmlTextWriter xmlWriter =
Please note that I first load original file and then I add new node. Use of
XmlDocument.AppendChild() is maybe better (more obvious). Anyway, I hope this is readable as well. Also note that I track inner exceptions by recursive calling
Exception.InnerException as argument.
There is of course a lot to do...
- case when more exceptions occurs in same time should be handled
- add more informations (like infos about currently logged users in your app)
- manipulating with XML could be done using
XmlDocumentFragment. It's more clear.
- generated XML is described by DTD. Expect use of XML Schema in next release.
- this one is difficult: automatic analysis of occured errors. but it would be great :)
- 14.7.2004 - Article first posted to CodeProject
My real name is David Nohejl, aka DNH. I am from Prague, Czech Republic. I am interested in many aspects of programming (databases, AI, algorithms...). My favourite programming language is C#, favourite IDE is, of course, VS.NET
. I love XML and all the things around it... Other interest than programming are science (math, physics, biology, psychology, paleontology) and basketball. Graduated Faculty of Mathematics and Physics
, Charles University in Prague as BSc. Working as programmer in company Unlimited ltd.