XML Trace Listener Problem

Topics: Logging Application Block
Apr 24, 2007 at 12:45 PM

We are using Logging Application Block in our application. With new EntLib 2007 we have added XML Trace listeneres and we have run into problem when reading XML file. If I open it in Visual Studio 2005 I recieve error

"XML document cannot contain multiple root level elements"

As I can see from XML there are multiple <E2ETraceEvent> tags and In Discussion http://www.codeplex.com/entlib/Thread/View.aspx?ThreadId=8871 that is not the case

In web.config file we have defined XML trace listener with EntLib conf tools and this is the conf xml code

<add fileName="D:\Projects\WordGeneratorSolution\WordGeneratorWS\Logs\LogRequests.xml"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.XmlTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.XmlTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="XMLRequests" />

We are using Visual Studio 2005 Pro and Enterprise Library April 2007

Any help would be apriciated

Apr 24, 2007 at 2:22 PM
I forgot about that error. I get it, too, when I try to open an XML TraceListener log file using XML NotePad.

The way I "fixed" it in the past is to open the file using NotePad and enter <Event> at the very beginning of the file and </Event> at the very end of the file which gives the editor its single root element.

I recommend adding this problem to the Issue Tracker so that the Ent Lib Team can include a fix in the next patch.




David Hayden
Microsoft MVP C#
Apr 24, 2007 at 3:12 PM
Add it to the issue tracker if you want, but I can tell you now it isn't likely to be fixed :-)
This is the behavior of the System.Diagnostics.XmlWriterTraceListener, which the EntLib XmlTraceListener wraps. While it seems strange that an XML Trace Listener doesn't generate valid XML, it does make some sense if you consider that it wouldn't be possible to keep appending to a file and maintain well-formed-ness. Theoretically you could build an XML Trace Listener that used the DOM or deleted the old closing element each time, but I'd imagine the performance of this solution would be too poor to consider in most cases.

Nov 27, 2007 at 12:47 PM
Check http://devlicio.us/blogs/bob_yexley/archive/2006/10/06/XML-log-file-with-Enterprise-Library.aspx to see how this can be used properly. You need to create a "wrapper" xml file which reads in the XML log file.