Skip to content
A Serilog sink that correlates log events with the code that produced them, enabling unit testing of log output.
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/Serilog.Sinks.TestCorrelator
test/Serilog.Sinks.TestCorrelator.Tests
.gitignore
LICENSE
NuGet.Config
README.md
appveyor.yml
serilog-sinks-testcorrelator.DotSettings
serilog-sinks-testcorrelator.sln

README.md

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!

You can’t perform that action at this time.