trbenning / serilog-sinks-xunit

The xunit test output sink for Serilog
MIT License
84 stars 9 forks source link

When running tests in parallel, log output related to one test will leak into other test output #6

Closed pseudobjorn closed 5 years ago

pseudobjorn commented 6 years ago

I am writing integration tests for a .NET Core 2.1 project (web api). I am using the latest version of all nuget packages (.net, xunit, serilog, etc). I have recently switched to serilog as the logging engine for my web api, and this works fine. However, in my integration tests, I thought it would be nice to see the logg-statements from the TestServer in the XUnit output window. In order to achieve this, I installed your nuget package, and created a logger as per your documentation.

This works perfectly fine, as long as I run a single test at a time. I can see the log statements that I trigger directly from my test-methods, as well as the log statements triggered within the actual webhost.

However, if I try to run all the tests in one go (XUnit will do this in parallell), the output from one test is leaked into the output for another! This makes sense to me, since Log.Logger after all is a globally shared logger.

Other Serilog sinks exist for xunit, and I see that a similar issue have been reported there, for example: https://github.com/davetimmins/serilog-sinks-xunittestoutput/issues/6

Are you able to reproduce this behaviour? It would be great if you could look into this!

trbenning commented 6 years ago

Thanks for reporting this. I'll look into it this weekend.

pseudobjorn commented 6 years ago

Hi Todd, any news on this?

TysonMN commented 6 years ago

This makes sense to me, since Log.Logger after all is a globally shared logger.

That is your issue. If your tests log to the global/static logger, then you will have race conditions that determine where the log events go.

To avoid this, you need to inject instance-specific Iloggers and send your log events to them instead.

trbenning commented 5 years ago

Functions as designed