在系统事件日志下有一个名为"服务控制管理器"的事件提供程序.它的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);
}
Run Code Online (Sandbox Code Playgroud)
我用"以管理员身份运行"运行应用程序.事件被写入系统日志,具有正确的事件ID,源等.但描述是"消息资源存在但消息未在字符串/消息表中找到",其中包含"测试服务进入%2状态" .
我的错是什么?
错误在于您无法实现这一点,WriteEntry因为您需要提供多个参数以及正确的EventIdentifier
如果您切换到WriteEvent您可以实现您所追求的目标:
var myNewLog = new EventLog("System", ".", "Service Control Manager");
myNewLog.WriteEvent( new EventInstance( (1 << 30) + 7036 ,0)
, null
, new object[] { "foobar","running" }
);
Run Code Online (Sandbox Code Playgroud)
请注意,Eventinstance 提供了一个 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6910 次 |
| 最近记录: |