我有一个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>
答案 0 :(得分:3)
看起来没有办法做到这一点,甚至没有反射: http://dotnetinside.com/en/framework/v4.0.30319/System/XmlWriterTraceListener
如果您可以使用单声道版本中的xml编写器设置,则可以: http://dotnetinside.com/en/framework/Mono+Framework/System/XmlWriterTraceListener
您可以使用服务跟踪查看器打开XML跟踪: http://msdn.microsoft.com/en-us/library/ms732023%28v=vs.110%29.aspx
否则,复制&amp;粘贴到您喜欢的IDE或XML编辑器。
答案 1 :(得分:0)
有点晚了但是如果你想在代码中这样做,下面的链接很好地展示了我们如何阅读XML。您可以阅读子树,然后可能反序列化为一个类。然后使用适当的设置将其序列化为XML。
我还从E2ETraceEvent Xml节点创建了C#类,以便反序列化Xml片段。
http://haishibai.blogspot.in/2009/04/monitoring-and-reading-svclog-file.html
示例代码:
using (var fileStream = new FileStream(logFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (XmlReader reader = new XmlTextReader(fileStream, XmlNodeType.Element, null))
{
while (reader.Read())
{
if (reader.Name == "E2ETraceEvent" && reader.NodeType == XmlNodeType.Element)
{
using (XmlReader subReader = reader.ReadSubtree())
{
while (subReader.Read())
{
var elementString = subReader.ReadOuterXml();
if (!string.IsNullOrWhiteSpace(elementString))
{
using (var xStream = this.GenerateStreamFromString(elementString))
{
XElement traceElement = XElement.Load(xStream);
var serializer = new XmlSerializer(typeof(E2ETraceEvent));
var traceObject = (E2ETraceEvent)serializer.Deserialize(traceElement.CreateReader());
var formattedXml = this.SerializeToXmlString<E2ETraceEvent>(traceObject);
}
}
}
}
}
}
}
====================================
private Stream GenerateStreamFromString(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
=====================================
private string SerializeToXmlString<T>(T objectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
var settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true
};
var xml = string.Empty;
using (var sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw, settings))
{
xmlSerializer.Serialize(writer, objectToSerialize);
xml = sw.ToString();
}
}
return xml;
}
======================================
希望这有帮助!
答案 2 :(得分:0)
您可以使用服务跟踪查看器工具(SvcTraceViewer.exe)来读取System.Diagnostics.XmlWriterTraceListener的输出。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/service-trace-viewer-tool-svctraceviewer-exe
它应该是Windows SDK(https://developer.microsoft.com/en-us/windows/downloads/windows-10-sdk)的一部分
并且可以在Microsoft SDKs目录下的某个位置找到。例如:
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\SvcTraceViewer.exe