Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EventLogSink is not thread safe #38

Open
wozni opened this issue Jun 18, 2020 · 1 comment
Open

EventLogSink is not thread safe #38

wozni opened this issue Jun 18, 2020 · 1 comment

Comments

@wozni
Copy link

wozni commented Jun 18, 2020

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

@nblumhardt
Copy link
Member

Just a note, this is an issue when constructing multiple event log sinks in parallel; a single event log sink is threadsafe.

Locking on something static in ConfigureSource() would resolve this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants