Serilog.Utilities.ConcurrentCorrelator

A library for unit testing Serilog log events in concurrent testing frameworks.


Keywords
serilog, log, logging, concurrent, concurrency, thread, threading, multithreading, unit, test, unittest, unittesting
License
MIT
Install
Install-Package Serilog.Utilities.ConcurrentCorrelator -Version 2.0.0.15

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!