gpt4 book ai didi

c# - 为什么此事件日志的 EntryWritten 事件永远不会触发?

转载 作者:行者123 更新时间:2023-11-30 18:45:14 26 4
gpt4 key购买 nike

我在 C# 控制台应用程序和 C# Windows 服务中都有以下代码。它在控制台应用程序中工作。它拾取指定的事件,并正确调用 MatchEvent()。 C# windows 服务中的相同代码不会拾取相同的指定事件,它永远不会看到它,但会看到其他事件。相关事件已写入应用程序日志,因此我不会尝试读取安全日志。

我认为这是一个帐户权限问题(该服务以 LocalSystem 身份运行)。我将服务更改为使用与运行控制台应用程序相同的帐户,但我仍然看到相同的行为。我确认没有对 GP 或自定义注册表执行任何更改权限的操作(这是一个全新安装的操作系统),并且两个应用程序使用的帐户都是本地管理员。

有什么我想念的吗?我也研究了 EventLogPermission,但这似乎并不适用,因为我正在从 eventLog 中获取事件。

代码:

private void WatchLogs()
{
try
{
_eventLogs = EventLog.GetEventLogs();

foreach (EventLog eventLog in _eventLogs)
{
if (eventLog.LogDisplayName.Contains("Security"))
{
_logger.DebugFormat(string.Format("{0}: not watching", eventLog.LogDisplayName));
}
else
{
eventLog.EntryWritten += EventLogEntryWritten;
eventLog.EnableRaisingEvents = true;

if (_logger.IsInfoEnabled)
{
_logger.InfoFormat("Monitoring: {0} | Raising Events: {1}", eventLog.LogDisplayName,
eventLog.EnableRaisingEvents);
}
}
}
}
catch (Win32Exception ee)
{
_logger.DebugFormat(string.Format("{0}: not watching({1})", eventLog.LogDisplayName, ee.Message));
}
catch (SecurityException securityException)
{
_logger.ErrorFormat("Error accessing eventlog: {0} : {1}", eventLog.LogDisplayName, securityException.Message);
}
}

private void EventLogEntryWritten(object sender, EntryWrittenEventArgs currentEvent)
{
var log = (EventLog) sender;

if (_logger.IsDebugEnabled)
_logger.DebugFormat(
"Event Raised: |Log:{0}|Source:{1}|EventID:{2}|",log.LogDisplayName,
currentEvent.Entry.Source,currentEvent.Entry.EventID);

MatchEvent(currentEvent);
}

最佳答案

是的,如果事件发生在轮询间隔内,您就会错过这些事件。您可以使用 WMI(通过 System.Management)来更改轮询间隔。 This是一篇较旧的文章,但它会为您提供执行此操作所需的信息。请参阅管理事件部分。

您还需要在单独的线程上执行 WriteEntry(如果您也在写入事件日志),这样就不会阻塞接收事件。

关于c# - 为什么此事件日志的 EntryWritten 事件永远不会触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/864273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com