Rolling XML File to Log Exception using Exception Handling

Topics: Exception Handling Application Block, Logging Application Block
Jan 20, 2012 at 1:29 PM


I want to write Exception to XML files (daily bases) using Exception Handling.

Moreover, I do not want XML Trace Listener to write all the exception details but few i.e. I want to skip nodes like System etc...

I think, I must not be only person who require such implementation. There must be easy solution to achieve this.

I am using Enterprise Libray 5.0 + .NET 4.0

Please guide me how can I achieve this.

Thanks in advance.

Jan 20, 2012 at 10:04 PM

Unfortunately the out of the box XmlTraceListener is hard coded to use the XmlLogFormatter which is what is writing out all of the properties.

You could create your own XmlTraceListener to output to the format that you want.  The actual XmlTraceListener code is only about 60 lines (since it uses System.Diagnostics.XmlWriterTraceListener for most of the functionality).  Then you would just have to write your own Xml Formatter for your desired format.

Since you would probably be writing a formatter anyway, you could create your own custom XmlFormatter and then use the (Rolling) FlatFileTraceListener  to write to disk.  This has the benefit of being able to change the trace listener (to event log or database) and still retain the XML format that you want.

If that is not interesting then a quick and simplistic approach would be to create a template that simulates the XML that you are interested in.  

E.g. <message>{message}</message><category>{category}</category>

The downside is that the file might not be well formed since the values won't be escaped for XML.

Yet another approach would be to create a class that represents the data that you want to log and then serialize that class to XML and set that serialized XML as the Message property.  Or a non-Enterprise Library helper class that formats a LogEntry the way you would like. You would then use a template that just outputs the message property  (i.e.: {message}).  


Creating a custom formatter is probably the best approach.

Randy Levy
Enterprise Library support engineer 

Jan 23, 2012 at 2:29 PM

Thanks Randy

I will go with the Custom Formatter.

Thanks for the help.

Mayank Kukadia