How to write log to SECURITY event Log in C#?

前提是你 提交于 2019-12-24 10:27:44

问题


I want to write these values in the security event log:

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);

I have an exception this line:

if (!EventLog.SourceExists(sSource))

Exception:

Cannot open log for source „Security”. You may not have write access.

But when I change the Security to another it works, but the just the application event log contains the values.


回答1:


Administrative privileges are required to read the Security Log so the SourceExists call will fail if not run under that context.

Additionally only LSA can write to the security log & it does not support types such as "Warning", only audit events.

Take a look at; http://msdn.microsoft.com/en-gb/magazine/cc163718.aspx



来源:https://stackoverflow.com/questions/11794013/how-to-write-log-to-security-event-log-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!