Serilog.Sinks.TestCorrelator

A Serilog sink that correlates log events with the code that produced them, enabling unit testing of log output.


Keywords
log, logging, serilog, sink, test, testing, unit, unittest, unittesting
License
MIT
Install
Install-Package Serilog.Sinks.TestCorrelator -Version 3.2.0

Documentation

Serilog.Sinks.TestCorrelator AppVeyor Badge NuGet Badge

A Serilog sink that correlates log events with the code that produced them, enabling unit testing of log output.

Usage

Just create a logger that writes or audits to the TestCorrelator.

Log.Logger = new LoggerConfiguration().WriteTo.TestCorrelator().CreateLogger();

Then wrap the code that you would like to monitor with a context and get the log events emitted within that context using the TestCorrelator.

using (TestCorrelator.CreateContext())
{
    Log.Information("My log message!");

    TestCorrelator.GetLogEventsFromCurrentContext()
        .Should().ContainSingle()
        .Which.MessageTemplate.Text
        .Should().Be("My log message!");
}

You can also get a stream of log events as an observable, which can be useful for testing long running or asynchronous tasks.

using (TestCorrelator.CreateContext())
{
    TestCorrelator.GetLogEventStreamFromCurrentContext()
        .Subscribe(logEvent => logEvent.MessageTemplate.Text.Should().Be("My log message!"));

    Log.Information("My log message!");
}

For more examples check out the unit tests!