serilog-sinks-testcorrelator
serilog-sinks-testcorrelator copied to clipboard
Support for testing multiple testcorrelator subloggers.
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();
}
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:
- Break each assert into its own test and add the
TestCorrelator
to one sub-logger per test. - Create your sub-loggers by calling Log.ForContext with a property you can filter by.
- In the example above you can just use two contexts.
There are ways we could support this behavior directly, but I'd have to give it a bit more thought.