C# Events at same time ignored

2019-09-04 08:57发布

问题:

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?

回答1:

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.



回答2:

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.