系统事件日志下有一个名为“服务控制管理器”的事件提供程序。它的EventMessageFile是%SystemRoot%\system32\services.exe。它包含一个id = 7036的事件,该事件是“进入%2状态的%1服务”。通过停止或运行services.msc中的任何服务,您可以非常简单地生成它。
我只想自己把这个事件写到系统事件日志中。
下面是我的简单日志代码:
public static void Main()
{
EventLog myNewLog = new EventLog("System", ".", "Service Control Manager");
myNewLog.WriteEntry("Test",EventLogEntryType.Information, 7036);
}我以“以管理员身份运行”运行应用程序。事件被写入具有正确的事件id、源等的系统日志。但是描述是“消息资源存在,但在”测试服务进入%2状态“的字符串/消息表中找不到消息。”
我犯了什么错?
发布于 2014-01-13 00:18:34
错误在于您不能使用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,它的最小16位为您找到的7036位,但位30 (Customer位)需要为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>https://stackoverflow.com/questions/6051279
复制相似问题