serilog-sinks-eventlog icon indicating copy to clipboard operation
serilog-sinks-eventlog copied to clipboard

EventLogSink is not thread safe

Open wozni opened this issue 4 years ago • 0 comments

I just discovered that creation of new event log and sources is not thread safe. It is failing during our tests as they are executed in parallel.

 if (System.Diagnostics.EventLog.SourceExists(source, log.MachineName))
{
	var existingLogWithSourceName = System.Diagnostics.EventLog.LogNameFromSourceName(source, log.MachineName);

	if (!string.IsNullOrWhiteSpace(existingLogWithSourceName) &&
		!log.Log.Equals(existingLogWithSourceName, StringComparison.OrdinalIgnoreCase))
	{
		// Remove the source from the previous log so we can associate it with the current log name
		System.Diagnostics.EventLog.DeleteEventSource(source, log.MachineName);
		oldLogName = existingLogWithSourceName;
	}
}
else
{
	System.Diagnostics.EventLog.CreateEventSource(sourceData);
}

https://github.com/serilog/serilog-sinks-eventlog/blob/dev/src/Serilog.Sinks.EventLog/Sinks/EventLog/EventLogSink.cs#L97

wozni avatar Jun 18 '20 09:06 wozni