2012-08-03 69 views
1

我想在安全事件日誌寫這些值:如何將日誌寫入安全事件登錄C#?

Console.WriteLine("Level: {0}", eventInstance.LevelDisplayName); 
Console.WriteLine("Date: {0}", eventInstance.TimeCreated); 
Console.WriteLine("Forrás: {0}", eventInstance.ProviderName); 
Console.WriteLine("Event id: {0}", eventInstance.Id); 
Console.WriteLine("Task: {0}", eventInstance.TaskDisplayName); 

string sSource; 
string sLog; 
string sEvent; 

sSource = eventInstance.ProviderName; 
sLog = "Security"; 
sEvent = eventInstance.FormatDescription(); 

if (!EventLog.SourceExists(sSource)) 
    EventLog.CreateEventSource(sSource, sLog); 

EventLog.WriteEntry(sSource, sEvent); 
EventLog.WriteEntry(sSource, sEvent, 
EventLogEntryType.Warning, eventInstance.Id); 

EventLog.WriteEntry(sSource, sEvent); 
EventLog.WriteEntry(sSource, sEvent, 
EventLogEntryType.Warning, eventInstance.Id); 

我有一個例外,這條線:

if (!EventLog.SourceExists(sSource)) 

例外:

無法打開日誌源「安全」。您可能沒有寫入權限。

但是,當我將Security更改爲另一個時,它可以正常工作,但只是應用程序事件日誌包含這些值。

+0

對不起,最後3行是該代碼的兩倍。 – user1520209 2012-08-03 10:39:30

回答

2

需要管理權限才能讀取安全日誌,因此如果不在該環境下運行,SourceExists調用將失敗。

此外只有LSA可以寫入安全日誌&它不支持類型,如「警告」,只有審計事件。

看看; http://msdn.microsoft.com/en-gb/magazine/cc163718.aspx