Serilog.Sinks.XUnit3 1.2.0

dotnet add package Serilog.Sinks.XUnit3 --version 1.2.0
                    
NuGet\Install-Package Serilog.Sinks.XUnit3 -Version 1.2.0
                    
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="Serilog.Sinks.XUnit3" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Serilog.Sinks.XUnit3" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Serilog.Sinks.XUnit3" />
                    
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 Serilog.Sinks.XUnit3 --version 1.2.0
                    
#r "nuget: Serilog.Sinks.XUnit3, 1.2.0"
                    
#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 Serilog.Sinks.XUnit3@1.2.0
                    
#: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=Serilog.Sinks.XUnit3&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Serilog.Sinks.XUnit3&version=1.2.0
                    
Install as a Cake Tool

alternate text is missing from this package README image

serilog-sinks-xunit-v3

The xUnit.v3 injectable test output sink for Serilog

Link to NuGet: https://www.nuget.org/packages/Serilog.Sinks.XUnit3

What is it?

It's a package that will allow you to use Serilog for test output in xUnit.v3.

Installation

dotnet CLI:

dotnet add package Serilog.Sinks.XUnit3

Package Reference:

<PackageReference Include="Serilog.Sinks.XUnit3" Version="1.0.0" />

Example usage

In Unit tests you can do not think about ITestOutputHelper, as it is backed by xUnit.v3 TestContext.Current.TestOutputHelper property. This implicit behaviour will not work with Integration ones due to TestHost being run in Background Thread.

Unit tests

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Serilog.Sinks.XUnit3.Tests.Unit;

public class UnitTestsOne
{
    [Fact]
    public void Do_Always_Success()
    {
        using var serviceProvider = new ServiceCollection()
            .AddLogging(x => x.ClearProviders()
                .AddSerilog(new LoggerConfiguration().WriteTo
                    .XUnit3TestOutput()
                    .CreateLogger()))
            .BuildServiceProvider();
        var logger = serviceProvider.GetRequiredService<ILogger<object>>();
        logger.LogInformation("Do");
    }
}

If you want to pass ITestOutputHelper and IMessageSink to XUnit3TestOutputSink manually, then you can do this:

var sink = new XUnit3TestOutputSink(Options.Create(new XUnit3TestOutputSinkOptions()))
{
    TestOutputHelper = testOutputHelper,
    MessageSink = messageSink
};

For more info read the docs: https://xunit.net/docs/capturing-output

Integration tests

using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Net.Http.Json;

namespace Serilog.Sinks.XUnit3.Tests.Integration;

[CollectionDefinition(nameof(SampleCollection))]
public class SampleCollection : ICollectionFixture<SampleFixture>;

public class SampleFixture : IAsyncLifetime
{
    public SampleFactory Factory { get; private set; } = null!;

    public SampleFixture SetTestOutputHelper(ITestOutputHelper testOutputHelper)
    {
        Factory.SetTestOutputHelper(testOutputHelper);
        return this;
    }

    public ValueTask InitializeAsync()
    {
        Factory = new SampleFactory();
        return ValueTask.CompletedTask;
    }

    public async ValueTask DisposeAsync() => await Factory.DisposeAsync();
}

public class SampleFactory : WebApplicationFactory<Program>
{
    protected override IHost CreateHost(IHostBuilder builder)
    {
        builder.ConfigureServices(services =>
        {
            services.AddSingleton(Options.Create(new XUnit3TestOutputSinkOptions()));
            services.AddSingleton<XUnit3TestOutputSink>();
        });
        builder.UseSerilog((_, serviceProvider, loggerConfiguration) =>
            loggerConfiguration.WriteTo.XUnit3TestOutput(
                serviceProvider.GetRequiredService<XUnit3TestOutputSink>()));

        return base.CreateHost(builder);
    }

    public void SetTestOutputHelper(ITestOutputHelper testOutputHelper) =>
        Services.GetRequiredService<XUnit3TestOutputSink>().TestOutputHelper = testOutputHelper;
}

[Collection(nameof(SampleCollection))]
public class SampleSuccessTests(SampleFixture fixture, ITestOutputHelper testOutputHelper)
{
    private readonly SampleFixture _fixture = fixture.SetTestOutputHelper(testOutputHelper);

    [Fact]
    public async Task Get_ById_Success()
    {
        var client = _fixture.Factory.CreateClient();
        var response = await client.GetFromJsonAsync<Todo>("/todos/1", CancellationToken.None);
        Assert.NotNull(response);
    }
    
    [Fact]
    public async Task Get_List_Success()
    {
        var client = _fixture.Factory.CreateClient();
        var response = await client.GetFromJsonAsync<Todo[]>("/todos", CancellationToken.None);
        Assert.NotNull(response);
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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 was computed.  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 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  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. 
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.2.0 7,788 11/13/2025
1.1.0 5,620 9/24/2025
1.0.0 533 9/22/2025