Serilog.Sinks.TestCorrelator
4.0.2
dotnet add package Serilog.Sinks.TestCorrelator --version 4.0.2
NuGet\Install-Package Serilog.Sinks.TestCorrelator -Version 4.0.2
<PackageReference Include="Serilog.Sinks.TestCorrelator" Version="4.0.2" />
<PackageVersion Include="Serilog.Sinks.TestCorrelator" Version="4.0.2" />
<PackageReference Include="Serilog.Sinks.TestCorrelator" />
paket add Serilog.Sinks.TestCorrelator --version 4.0.2
#r "nuget: Serilog.Sinks.TestCorrelator, 4.0.2"
#:package Serilog.Sinks.TestCorrelator@4.0.2
#addin nuget:?package=Serilog.Sinks.TestCorrelator&version=4.0.2
#tool nuget:?package=Serilog.Sinks.TestCorrelator&version=4.0.2
Serilog.Sinks.TestCorrelator

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!");
}
New in v4: If you have more than one logger and want to filter to only log events emitted to a specific sink you can pass in an ID that you then use to filter the log events.
TestCorrelatorSinkId firstTestCorrelatorSinkId = new();
ILogger firstLogger = new LoggerConfiguration().WriteTo.TestCorrelator(firstTestCorrelatorSinkId).CreateLogger();
TestCorrelatorSinkId secondTestCorrelatorSinkId = new();
ILogger secondLogger = new LoggerConfiguration().WriteTo.TestCorrelator(secondTestCorrelatorSinkId).CreateLogger();
using (TestCorrelator.CreateContext())
{
firstLogger.Information("My first log message!");
secondLogger.Information("My second log message!");
TestCorrelator.GetLogEventsForSinksFromCurrentContext(firstTestCorrelatorSinkId)
.Should().ContainSingle()
.Which.MessageTemplate.Text
.Should().Be("My first log message!");
TestCorrelator.GetLogEventsForSinksFromCurrentContext(secondTestCorrelatorSinkId)
.Should().ContainSingle()
.Which.MessageTemplate.Text
.Should().Be("My second log message!");
}
For more examples check out the unit tests!
v4 Breaking Changes
Version 4 comes with a few breaking changes.
- All package dependencies have been updated.
- Dropped support for .Net Framework 4.6.1 and below. This brings the package in line with Serilog's supported frameworks.
- A new
TestCorrelatorContextIdclass replacesGuids. The corresponding...ContextGuid()methods have been renamed to...ContextId(). LogEvents emitted outside of a context are no longer captured.LoggerConfigurationextension methods with implicitly ignored minimumLogEventLevelparameters were removed.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 is compatible. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Serilog (>= 3.1.1)
- System.Collections.Immutable (>= 8.0.0)
- System.Reactive (>= 6.0.1)
-
.NETFramework 4.7.1
- Serilog (>= 3.1.1)
- System.Collections.Immutable (>= 8.0.0)
- System.Reactive (>= 6.0.1)
-
.NETStandard 2.0
- Serilog (>= 3.1.1)
- System.Collections.Immutable (>= 8.0.0)
- System.Reactive (>= 6.0.1)
-
net10.0
- Serilog (>= 3.1.1)
- System.Reactive (>= 6.0.1)
-
net6.0
- Serilog (>= 3.1.1)
- System.Collections.Immutable (>= 8.0.0)
- System.Reactive (>= 6.0.1)
-
net8.0
- Serilog (>= 3.1.1)
- System.Reactive (>= 6.0.1)
-
net9.0
- Serilog (>= 3.1.1)
- System.Reactive (>= 6.0.1)
NuGet packages (5)
Showing the top 5 NuGet packages that depend on Serilog.Sinks.TestCorrelator:
| Package | Downloads |
|---|---|
|
CUSTIS.NetCore.Tests
Утилиты для тестов |
|
|
PureActive.Serilog.Sink.Xunit
PureActive Serilog Sink for xUnit |
|
|
OutWit.Common.Logging
Logging aspect support for MVVM |
|
|
Rdh.TestHelper
Helper for Unit tests |
|
|
Base.Logging
A logging utility that sanitizes JSON data to redact sensitive information. |
GitHub repositories (4)
Showing the top 4 popular GitHub repositories that depend on Serilog.Sinks.TestCorrelator:
| Repository | Stars |
|---|---|
|
BrighterCommand/Brighter
A framework for building messaging apps with .NET and C#.
|
|
|
Reaparr/Reaparr
Plex downloader that brings content from any server to yours!
|
|
|
planetarium/libplanet
Blockchain in C#/.NET for on-chain, decentralized gaming
|
|
|
dougdellolio/coinbasepro-csharp
The unofficial .NET/C# client library for the Coinbase Pro/GDAX API
|