C# 同时忽略事件

发布于 2024-10-14 06:41:01 字数 388 浏览 8 评论 0原文

我目前遇到了我不久前创建的事件日志监视器的问题。

我的应用程序订阅 EventLog 的 EntryWritten 事件。 我注意到,如果多个事件同时发生(同一秒内),则只有其中一个事件会引发触发我的事件处理程序的事件。

_eventLog = new System.Diagnostics.EventLog(_logName);
_eventLog.EntryWritten += new EntryWrittenEventHandler(eventlog_EntryWritten);
_eventLog.EnableRaisingEvents = true;

是否可以避免这种行为,以便为记录到事件日志的所有事件调用我的 eventlog_EntryWritten 方法?

I am currently running into a problem with an eventlog monitor I created a while ago.

My application subscribes to the EntryWritten events of the EventLog.
I noticed that if multiple events occurs at the same time (within the same second), only one of them raises the event which triggers my eventhandler.

_eventLog = new System.Diagnostics.EventLog(_logName);
_eventLog.EntryWritten += new EntryWrittenEventHandler(eventlog_EntryWritten);
_eventLog.EnableRaisingEvents = true;

Is it possible to avoid this behavior, so my eventlog_EntryWritten method will be called for all events that are logged to the eventlog?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

琉璃梦幻 2024-10-21 06:41:02

比这更糟糕 - 它在五秒的间隔内:

仅当最后一次写入事件至少在五秒之前发生时,系统才会响应 WriteEntry。这意味着即使发生多个事件日志更改,您也只会在五秒间隔内收到一个 EntryWritten 事件通知。

(来自 EventLog.EntryWritten

如果您需要跟踪所有事件(如果它们是您的),我建议在 WriteEntry 周围添加一个包装函数,该函数可以在所有条目写入日志之前捕获它们。

It's worse than that - it's within a five second interval:

The system responds to WriteEntry only if the last write event occurred at least five seconds previously. This implies you will only receive one EntryWritten event notification within a five-second interval, even if more than one event log change occurs.

(from EventLog.EntryWritten)

If you need to track all events then (if they're yours), I'd suggest adding a wrapper function around WriteEntry that can catch all of the entries before they're even written to the log.

心作怪 2024-10-21 06:41:02

事件通常不会在异步模式下运行,因此如果 eventlog_EntryWritten 没有在另一个事件之前完成,您可能会错过这些事件。在“即发即忘”方法中引发您自己的异步事件。我不确定,但这可能会更有效。或者让您自己的类使用轮询机制来读取事件。

Events usually dont run in Async mode, and so if eventlog_EntryWritten does not finish before another event, you probably miss the events. Raise your own async event in fire and forget method. I am not sure, but probably this will be more effective. Or have your own class using polling mechanism to read the events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文