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 Strong name sign the assembly. Apr 2, 2019
test/Serilog.Sinks.TestCorrelator.Tests The_LogEvent_stream_for_the_current_context_does_not_create_notificat… Dec 8, 2018
.gitignore Ignore nCrunch files. Mar 10, 2017
LICENSE Transfer copyright. Jan 18, 2019
NuGet.Config Did it right this time. I think. Apr 11, 2017
README.md
appveyor.yml Update minor version. Apr 2, 2019
serilog-sinks-testcorrelator.DotSettings Rename resharper settings file. Aug 11, 2017
serilog-sinks-testcorrelator.sln Rename solution. Aug 6, 2017

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.