MitchBodmer / serilog-sinks-testcorrelator

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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!

About

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

License:MIT License


Languages

Language:C# 100.0%