将.NET EventLogEntry实例序列化为XML [英] Serializing a .NET EventLogEntry instance to XML
问题描述
在Windows 7中使用事件查看器时,可以从事件属性"对话框中访问事件的单独的"XML视图".此XML引用http://schemas.microsoft.com/win/2004/08/events/event
命名空间.
When using the Event Viewer in Windows 7, there is a separate 'XML View' of an event that can be accessed from the Event Properties dialog. This XML refers to the http://schemas.microsoft.com/win/2004/08/events/event
namespace.
当我在System.Diagnostics
命名空间中使用.NET框架类订阅Windows事件并以EventLogEntry
实例的形式检索事件对象时,是否可以将这些实例序列化为上述XML格式?我似乎找不到任何东西.
When I subscribe to Windows Events using the .NET framework classes in the System.Diagnostics
namespace and retrieve event objects in the form of EventLogEntry
instances, is there a way to serialize these instances to the XML format mentioned above? I can not seem to find any.
非常感谢您的回复.
更新:感谢jmservera,我发现System.Diagnostics.Eventing.Reader
名称空间中存在另一个更好的API,但是该API不支持部署到Windows Server 2003/XP.
Update: thanks to jmservera I have found out there is a different and better API in the System.Diagnostics.Eventing.Reader
namespace, however this API does not support deployment to Windows Server 2003/ XP.
更新2 :我接受了jmservera的回答,因为它使我找到了解决方案.如果您的目标是Vista/Windows Server 2008,请遵循jmservera的建议,并在较新的名称空间中使用API.但是,如果需要支持以前的操作系统,则必须使用较旧的API,并将EventLogEntry序列化为XML.
Update 2: I have accepted jmservera's answer, because it has lead me to the solution. If you are targetting Vista/ Windows Server 2008 follow jmservera's suggestion and use the API in the newer namespace. If, however, you need to support previous OS's you will have to use the older API and serialize the EventLogEntry to XML yourself.
推荐答案
您必须使用System.Diagnostics.Eventing.Reader命名空间,如下所示:
You have to use the System.Diagnostics.Eventing.Reader namespace like this:
static void Main(string[] args)
{
EventLogQuery query = new EventLogQuery("System", PathType.LogName);
EventLogWatcher watcher = new EventLogWatcher(query);
watcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>(watcher_EventRecordWritten);
watcher.Enabled = true;
Console.ReadLine();
}
static void watcher_EventRecordWritten(object sender, EventRecordWrittenEventArgs e)
{
Console.WriteLine(e.EventRecord.ToXml());
}
这篇关于将.NET EventLogEntry实例序列化为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!