如何在Windows中向EventLog添加多行EventData?

Ano*_*oop 5 c# event-log

我目前能够使用以下代码创建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);
Run Code Online (Sandbox Code Playgroud)

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

bat*_*tar 6

一个班轮应该是这样的:

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

这里应用程序是事件源,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>
Run Code Online (Sandbox Code Playgroud)


Rem*_*eau -2

只需在消息字符串中添加换行符即可记录多行文本。