trbenning/serilog-sinks-xunit

Logging to TestOutput without creating new logger each time

Opened this issue · 2 comments

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.

@michaeltg17 do you have a public example or more details of you code?
I am in the situation you are describing.
Where is the TestOutputHelper coming from?

@michaeltg17 do you have a public example or more details of you code? I am in the situation you are describing. Where is the TestOutputHelper coming from?

The TestOutputHelper is an ITestOutputHelper property in the fixture class. It is set by the test class because it's there where you have the ITestOutputHelper injected by xUnit and it changes for every test.

I have some public code but my case is more complex because I'm using a base class and property injection using https://github.com/pengweiqhca/Xunit.DependencyInjection. I use property injection so that my tests don't need constructors passing the ITestOutputHelper and/or the fixture to the base class in each test class.

My code is here in the integration tests project:

https://github.com/michaeltg17/Public.Api