No1.SerilogTestSink 1.0.2

dotnet add package No1.SerilogTestSink --version 1.0.2
                    
NuGet\Install-Package No1.SerilogTestSink -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="No1.SerilogTestSink" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="No1.SerilogTestSink" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="No1.SerilogTestSink" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add No1.SerilogTestSink --version 1.0.2
                    
#r "nuget: No1.SerilogTestSink, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package No1.SerilogTestSink@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=No1.SerilogTestSink&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=No1.SerilogTestSink&version=1.0.2
                    
Install as a Cake Tool

No1.SerilogTestSink

With SerilogTestSink, you can check wether specific message is being logged or not.

Usage

Add SerilogTestSink to your serilog configuration:

Add No1.SerilogTestSink to project's dependencies. Add below lines to your .csproj file:

<ItemGroup Condition="'$(IsTestProject)' == 'true'">
	<PackageReference Include="No1.SerilogTestSink" Version="1.0.0.1" />
</ItemGroup>

Please note this dependency only when added to dependencies that project is being tested. Note to (IsTestProject variable)

Then add below configs in your test configuration. For example in appsettings.test.json:

"Serilog": {
	"Using": [ "No1.SerilogTestSink" ],
	"WriteTo": [
		{ "Name": "TestSerilog" }
	]
}

Now you can check logged messages in your tests like:

[Fact]
public async Task WhenDebugMessageLogPostedThenLoggerSkips() {
	// Arrange
	var request = new HttpRequestMessage(HttpMethod.Get, "/LocalOnly/Test/" + nameof(TestController.LogDebug));

	// Act
	var response = await client.SendAsync(request);

	// Assert
	Assert.True(TestSerilogSink.LogEvents.Any());
	Assert.DoesNotContain(TestSerilogSink.LogEvents, x => x.Match<TestLogTemplates>(nameof(TestLogTemplates.LogDebug)));
}

[Fact]
public async Task WhenInfromationMessageLogPostedThenLoggerPersist() {
	// Arrange
	var request = new HttpRequestMessage(HttpMethod.Get, "/LocalOnly/Test/" + nameof(TestController.LogInformation));

	// Act
	var response = await client.SendAsync(request);

	// Assert
	Assert.Contains(TestSerilogSink.LogEvents, x => x.Match<TestLogTemplates>(nameof(TestLogTemplates.LogInformation)));
}

[Fact]
public async Task WhenAnEndpointThrowsExceptionThenExceptionWillBeLogged() {
	// Arrange
	var request = new HttpRequestMessage(HttpMethod.Get, "/LocalOnly/Test/" + nameof(TestController.ThrowException));

	// Act
	var response = await client.SendAsync(request);

	// Assert
	Assert.Contains(TestSerilogSink.LogEvents, x => x.Level == LogEventLevel.Error && x.Exception?.GetType() == typeof(Exception));
}

What is method Match?

It is optimized to define a method for eveny log message like below:

internal partial class TestLogTemplates
{
	protected TestLogTemplates()
	{
	}

	[LoggerMessage(Level = LogLevel.Trace, Message = "LogTrace is being called at {instant}")]
	internal static partial void LogTrace(ILogger logger, Instant instant);

	[LoggerMessage(Level = LogLevel.Debug, Message = "LogDebug is being called at {instant}")]
	internal static partial void LogDebug(ILogger logger, Instant instant);

	[LoggerMessage(Level = LogLevel.Information, Message = "LogInformation is being called at {instant}")]
	internal static partial void LogInformation(ILogger logger, Instant instant);

	[LoggerMessage(Level = LogLevel.Warning, Message = "LogWarning is being called at {instant}")]
	internal static partial void LogWarning(ILogger logger, Instant instant);

	[LoggerMessage(Level = LogLevel.Error, Message = "LogError is being called at {instant}")]
	internal static partial void LogError(ILogger logger, Instant instant);

	[LoggerMessage(Level = LogLevel.Critical, Message = "LogCritical is being called at {instant}")]
	internal static partial void LogCritical(ILogger logger, Instant instant);
}

So I used this methods to post log messages and then used below method to make my tests more clear. It is up to you to use it or not:

LogEventExtensions.Match

Building

For enabling Husky.NET run:

dotnet new tool-manifest
dotnet tool install Husky
git add .husky/pre-commit

After this, Husky formats the code in staged files based on .editorconfig configurations just before commit.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 311 6/14/2026