我的跟踪侦听器中添加了一个XMLWriterTraceListener对象,
System.Diagnostics.XmlWriterTraceListener xmlTrace = new
System.Diagnostics.XmlWriterTraceListener("Trace.xml");
xmlTrace.IndentLevel = 1;
xmlTrace.IndentSize = 4;
System.Diagnostics.Trace.Listeners.Add(xmlTrace);
它正在成功地接收来自trace.writeline()、trace.traceInformation()等的消息。唯一的问题是它正在写入的trace.xml文件不可读,因为它不包含换行符或缩进。我是否缺少中间步骤(xmlstreamwriter?)那会增加产量吗?
当前输出为:
<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>
但我想读一下:
<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>
最佳答案
似乎没有任何方法可以做到这一点,即使是反射:
http://dotnetinside.com/en/framework/v4.0.30319/System/XmlWriterTraceListener
如果您可以在mono版本中获取xml writer设置,则可以:
http://dotnetinside.com/en/framework/Mono+Framework/System/XmlWriterTraceListener
可以使用服务跟踪查看器打开XML跟踪:
http://msdn.microsoft.com/en-us/library/ms732023%28v=vs.110%29.aspx
否则,请复制并粘贴到您喜爱的IDE或XML编辑器。