Click here to Skip to main content
13,352,631 members (35,076 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 13 Jul 2004

Simple XML based Error Log

, 13 Jul 2004
Rate this:
Please Sign up or sign in to vote.
This article shows how to write and use simple XML-based error log

Sample Image - XMLErrorLog.gif


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 XmlTextWriter .

This article provides some basics of:

  • writing XML files using System.Xml.XmlTextWriter
  • handling exceptions using try-catch
  • detecting unhandled exceptions using Server.GetLastError()

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, ErrorLog 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, Application_Error handler).

This is handler in Global.aspx.cs:

protected void Application_Error(Object sender, EventArgs e)
    // log unhandled exception
    // it's late, but better do something!

Points of Interest

This code writes data from Exception into XML writer:

private void WriteException(Exception e, XmlWriter xmlWriter)
        System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
    // write the description of exeption

    // method where exception was thrown
    // help link
    // call stack trace
        // recursively writes inner exceptions

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;
           //Moves the reader to the root element.
           // skip <?xml version="1.0" ...
           strOriginal = xmlReader.ReadInnerXml();
           // write new document
           XmlTextWriter xmlWriter =
new System.Xml.XmlTextWriter(m_logFileName,System.Text.Encoding.UTF8);
           // write original content
           //write down new exception

Please note that I first load original file and then I add new node. Use of XmlDocument and 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 WriteException() with 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 XmlDocument or 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


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Student Charles University in Prague
Czech Republic Czech Republic
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 Smile | :) . 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.. Doing cool stuff with C#, WPF, Javascript.

You may also be interested in...

Comments and Discussions

Questionneed thread safe version asap Pin
Ali_MH22-May-07 6:30
memberAli_MH22-May-07 6:30 
AnswerRe: need thread safe version asap Pin
dnh26-May-07 3:50
memberdnh26-May-07 3:50 
GeneralObsolete Pin
SORUOPNEHPETS10-Dec-06 12:16
memberSORUOPNEHPETS10-Dec-06 12:16 
GeneralHai Pin
Praphulla23-Dec-04 2:53
memberPraphulla23-Dec-04 2:53 
GeneralRe: Hai Pin
dnh23-Dec-04 4:31
memberdnh23-Dec-04 4:31 
GeneralRe: Hai Pin
Praphulla23-Dec-04 20:17
memberPraphulla23-Dec-04 20:17 
Make time to upload.Smile | :)
and let me know the same
GeneralRe: Hai Pin
vzcharlie6-Jan-05 21:04
membervzcharlie6-Jan-05 21:04 
GeneralRe: Hai Pin
dnh8-Jan-05 12:56
memberdnh8-Jan-05 12:56 
Questionthread safety? Pin
Eric Engler16-Jul-04 8:40
memberEric Engler16-Jul-04 8:40 
AnswerRe: thread safety? Pin
dnh16-Jul-04 9:04
memberdnh16-Jul-04 9:04 
Generallog4net Pin
Uwe Keim14-Jul-04 20:42
sitebuilderUwe Keim14-Jul-04 20:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 14 Jul 2004
Article Copyright 2004 by DavidNohejl
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid