2011-05-18 73 views
6

在系统事件日志下有一个名为“服务控制管理器”的事件提供程序。它的EventMessageFile是%SystemRoot%\system32\services.exe。它包含一个id = 7036的事件,而这个事件是“%1服务进入%2状态”。您可以通过停止或运行services.msc中的任何服务来生成它。消息资源存在,但在字符串/消息表中找不到该消息

我想要的就是自己将该事件写入系统事件日志。

这里是我的简单的日志记录代码:

public static void Main() 
{  
    EventLog myNewLog = new EventLog("System", ".", "Service Control Manager"); 

    myNewLog.WriteEntry("Test",EventLogEntryType.Information, 7036); 
} 

我运行“以管理员身份运行”的应用程序。使用正确的事件ID,源等将事件写入到系统日志中。但是,“测试服务进入%2状态”的描述是“消息资源存在,但在字符串/消息表中找不到消息” 。

我的错误是什么?

回答

1

的错误是,你不能做到这一点与WriteEntry因为你需要提供多个参数以及正确的EventIdentifier

如果切换到WriteEvent就可以实现你在哪里后:

var myNewLog = new EventLog("System", ".", "Service Control Manager"); 

myNewLog.WriteEvent(new EventInstance((1 << 30) + 7036 ,0) 
        , null 
        , new object[] { "foobar","running" } 
        ); 

请注意事件标识符是由一个EventIdentifier提供的,该EventIdentifier在其最低16位中具有您找到的7036,但位30(客户位)需要为1,表示我们有客户代码。

运行这段代码以管理员身份给出了事件日志:

的foobar的服务进入运行状态。

与该XML:

<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> 
    <System> 
    <Provider Name="Service Control Manager" Guid="{some-guid-here}" EventSourceName="Service Control Manager" /> 
    <EventID Qualifiers="16384">7036</EventID> 
    <Version>0</Version> 
    <Level>4</Level> 
    <Task>0</Task> 
    <Opcode>0</Opcode> 
    <Keywords>0x80000000000000</Keywords> 
    <TimeCreated SystemTime="2014-01-13T00:13:56.000000000Z" /> 
    <EventRecordID>999999</EventRecordID> 
    <Correlation /> 
    <Execution ProcessID="0" ThreadID="0" /> 
    <Channel>System</Channel> 
    <Computer>internal.example.com</Computer> 
    <Security /> 
</System> 
<EventData> 
    <Data Name="param1">foobar</Data> 
    <Data Name="param2">running</Data> 
    <Binary /> 
</EventData> 
</Event>