2011-05-18 692 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>