gpt4 book ai didi

c# - 如何在 Windows 中将多行 EventData 添加到 EventLog?

转载 作者:太空狗 更新时间:2023-10-29 23:10:14 25 4
gpt4 key购买 nike

我目前能够使用以下代码创建 Windows 事件日志:

    string sSource;
string sLog;
string sEvent;
sSource = "Sample App";
sLog = "Application";
sEvent = "Sample Event";

if (!EventLog.SourceExists(sSource))
EventLog.CreateEventSource(sSource,sLog);

EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Warning, 11111);

这会在应用程序日志中创建一个日志。我想在事件日志中的事件中添加多行数据,以便在调试时我可以直接解析日志以查找问题。另外,我查看了应用程序日志中的其他一些日志,它们中似乎有一个二进制数据字段。我无法弄清楚如何编写这样一个字段,因为上面的代码只添加了一个 EventData 字段。

最佳答案

一个类轮应该是这样的:

EventLog.WriteEvent("Application", new EventInstance(123, 0, EventLogEntryType.Information), new object[] { "Entry1" , "Entry2" });

这里Application是事件源,123是事件Id,0 = NONE是事件类别。您可能需要先检查事件源是否存在。

事件是这样的:

 <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Application" />
<EventID Qualifiers="0">1001</EventID>
<Level>4</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2015-07-12T21:26:07.000000000Z" />
<EventRecordID>86554</EventRecordID>
<Channel>Application</Channel>
<Computer>YOUR_COMPUTER</Computer>
<Security />
</System>
<EventData>
<Data>Entry1</Data>
<Data>Entry2</Data>
</EventData>
</Event>

关于c# - 如何在 Windows 中将多行 EventData 添加到 EventLog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7694276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com