【问题标题】:How to write log to SECURITY event Log in C#?如何在 C# 中将日志写入安全事件日志?
【发布时间】:2012-08-01 09:37:54
【问题描述】:

我想在安全事件日志中写入这些值:

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 更改为另一个时,它可以工作,但只是应用程序事件日志包含这些值。

【问题讨论】:

  • 抱歉,最后 3 行在该代码中出现了两次。

标签: c# security events logging event-log


【解决方案1】:

需要管理员权限才能读取安全日志,因此如果不在该上下文中运行,SourceExists 调用将失败。

此外,只有 LSA 可以写入安全日志,它不支持“警告”等类型,仅支持审计事件。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多