serilog-sinks-xunit
serilog-sinks-xunit copied to clipboard
Logging to TestOutput without creating new logger each time
Hi.
In the example of the readme file, a logger is created each time per test. When testing ASP.NET Core Apis using the WebApplicationFactory, you probably have a collection fixture to initialize the api once and dispose it at the end of the tests. When configuring the api, you set up the serilog once. The problem is that the ITestOutputHelper changes for every test, and when executing several of them, only the first one is logged.
I solved it using the map sink like this:
configuration.WriteTo.Map(
_ => TestOutputHelper,
(_, writeTo) => writeTo.TestOutput(TestOutputHelper),
sinkMapCountLimit: 1);
I think it would be nice to have a sample in the readme file for this case.