Description
Description
In the RequestEvents method of the EventLogWatcher class, an infinite loop may occur. This can happen if the NativeWrapper.EvtNext call returns true on the first attempt, indicating that not all log entries have been read. If an exception occurs during the next iteration of the loop, for example, due to the remote computer disconnecting from the network, the result variable will never be reset to false, leading to an infinite loop.
Reproduction Steps
This issue is challenging to describe concisely, as reproducing it depends on an exception occurring when connecting to a remote computer. In general terms, the scenario looks like this:
using System.Diagnostics.Eventing.Reader;
...
EventLogSession session = new EventLogSession("remote-pc");
EventLogQuery query = new EventLogQuery("Application", PathType.LogName) { Session = session };
EventLogWatcher watcher = new EventLogWatcher(query, null, true);
watcher.EventRecordWritten += (s, e) => {
if (e.EventException != null) {
logger.LogError(e.EventException, "Error occurs on an attempt to read log");
} else if (e.EventRecord is EventLogRecord record) {
logger.LogInformation("Log entry has been read");
}
};
watcher.Enabled = true;
After enabling the watcher, but before all log entries have been read, the remote computer (remote-pc) must disconnect from the network. This triggers an infinite loop.
Expected behavior
After an exception occurs, I expect the watcher to either wait for the computer to reconnect to the network or simply transition to a disabled state.
Actual behavior
The EventRecordWritten event handler triggers an infinite number of times, causing the program to hang.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response