Search code examples
c#xmlloggingtracelistener

XMLWriterTraceListener produces wall-of-text instead of formatted XML


I've got an XMLWriterTraceListener object added to my trace listeners like so,

System.Diagnostics.XmlWriterTraceListener xmlTrace = new 
    System.Diagnostics.XmlWriterTraceListener("Trace.xml");
xmlTrace.IndentLevel = 1;
xmlTrace.IndentSize = 4;

System.Diagnostics.Trace.Listeners.Add(xmlTrace);

and it is successfully receiving messages from Trace.WriteLine(), Trace.TraceInformation(), etc. The only problem isthat the Trace.xml file it is writing to is humanly unreadable since it contains no line breaks or indentation. Am I missing an intermediary step (XMLStreamWriter?) that will pretty up the output?

The current output reads:

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"><System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"><EventID>0</EventID><Type>3</Type><SubType Name="Information">0</SubType><Level>8</Level><TimeCreated SystemTime="2014-03-20T18:05:43.2778822Z" /><Source Name="Program.vshost.exe" /><Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" /><Execution ProcessName="Program.vshost" ProcessID="6840" ThreadID="9" /><Channel/><Computer>ODYSSEY</Computer></System><ApplicationData>Startup!</ApplicationData></E2ETraceEvent>

but I'd like it to read:

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
    <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
        <EventID>0</EventID>
        <Type>3</Type>
        <SubType Name="Information">0</SubType>
        <Level>8</Level>
        <TimeCreated SystemTime="2014-03-20T18:05:43.2778822Z" />
        <Source Name="Program.vshost.exe" />
        <Correlation ActivityID="{00000000-0000-0000-0000-000000000000}" />
        <Execution ProcessName="Program.vshost" ProcessID="6840" ThreadID="9" />
        <Channel/>
        <Computer>ODYSSEY</Computer>
    </System>
    <ApplicationData>Startup!</ApplicationData>
</E2ETraceEvent>

Solution

  • Doesn't look like there is any way to do this, not even with reflection: http://dotnetinside.com/en/framework/v4.0.30319/System/XmlWriterTraceListener

    You might be able to if you can get at the xml writer settings in the mono version: http://dotnetinside.com/en/framework/Mono+Framework/System/XmlWriterTraceListener

    You can open XML trace with the Service Trace Viewer: http://msdn.microsoft.com/en-us/library/ms732023%28v=vs.110%29.aspx

    Otherwise, copy & paste to your favorite IDE or XML editor.