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

Support for testing multiple testcorrelator subloggers.

Open leejeffr opened this issue 5 years ago • 2 comments

Is there a way to test sublogging using TestCorrelator?

For example, I want to verify that some logging events go to subLog1 and others go to sublog2.

What would happen below if both of my subloggers were of type TestCorrelator? I believe this test will fail because there will be two logevents. I'd like to test my filtering on subloggers and it seems reasonable to test that one sublogger recieved a particular event and other one didn't.

using (TestCorrelator.CreateContext())
{
    log.Debug("This is a test {A}", "ABC");
    log.Debug("Debug Sublogger");

    TestCorrelator.GetLogEventsFromCurrentContext().Should().ContainSingle();
}

leejeffr avatar Oct 22 '19 17:10 leejeffr

There isn't a built in way for a sink to determine what called Emit on it. I think there are a few ways of handling this:

  1. Break each assert into its own test and add the TestCorrelator to one sub-logger per test.
  2. Create your sub-loggers by calling Log.ForContext with a property you can filter by.
  3. In the example above you can just use two contexts.

MitchBodmer avatar Oct 22 '19 17:10 MitchBodmer

There are ways we could support this behavior directly, but I'd have to give it a bit more thought.

MitchBodmer avatar Oct 22 '19 17:10 MitchBodmer