Serilog.Sinks.XUnit.Injectable 4.0.181

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

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

Serilog.Sinks.XUnit.Injectable

The injectable, Serilog xUnit test output sink

Leverage xUnit's TestOutputHelper across tests that share state

Common use cases

  • Integration tests (i.e. WebApplicationFactory)
  • Unit tests that utilize Dependency Injection in a fixture

Why?

When running a suite of tests, it can be expensive to build a new DI ServiceProvider, and even more so, a WebApplicationFactory. Hence, these can be stored in a xUnit fixture and reused across tests.

xUnit provides a new TestOutputHelper per test, and so even if you register it as a sink initially, the next test will not capture/output messages from the services inside the provider.

This library addresses that issue by allowing for the TestOutputHelper from each test to be "injected" into the fixture as it's running. It also maintains the context of each test in the appropriate test runner window.

Examples are provided for both Unit and Integration tests. For brevity, the actual injection is shown in the constructor of the test class in the examples below, but you'll probably want to leverage a base class.

xUnit Compatibility

  • Version 3.x: Supports xUnit 2.9.
  • Latest Version: Fully supports xUnit 3.

Installation

dotnet add package Serilog.Sinks.XUnit.Injectable

Example: WebApplicationFactory "integration tests"


public class ApiFixture : IAsyncLifetime
{
    public WebApplicationFactory<Program> ApiFactory { get; set; } = default!;

    public Task InitializeAsync()
    {
        ApiFactory = new WebApplicationFactory<Program>();
        ApiFactory = ApiFactory.WithWebHostBuilder(builder =>
        {
            // Instantiate the sink, with any configuration (like outputTemplate, formatProvider)
            var injectableTestOutputSink = new InjectableTestOutputSink();

            builder.ConfigureServices(services =>
            {
                // Register the sink as a singleton
                services.AddSingleton<IInjectableTestOutputSink>(injectableTestOutputSink); 
            });

            builder.UseSerilog((_, loggerConfiguration) =>
            {
                // Add the sink to the logger configuration
                loggerConfiguration.WriteTo.InjectableTestOutput(injectableTestOutputSink);
            });
        });

        return Task.CompletedTask;
    }

    public async Task DisposeAsync()
    {
        await ApiFactory.DisposeAsync();
    }
}
[Collection("ApiCollection")]
public class ApiTests
{
    private readonly HttpClient _client;

    public ApiTests(ApiFixture fixture, ITestOutputHelper testOutputHelper)
    {
        var outputSink = (IInjectableTestOutputSink)fixture.ApiFactory.Services.GetService(typeof(IInjectableTestOutputSink))!;
        outputSink.Inject(testOutputHelper); // <-- inject the new ITestOutputHelper into the sink

        _client = fixture.ApiFactory.CreateClient();
    }

    [Fact]
    public async Task Get_should_have_log_messages_and_be_successful()
    {
        HttpResponseMessage response = await _client.GetAsync("/");
        response.EnsureSuccessStatusCode();
    }
}

Example: ServiceProvider "Fixtured unit tests"


public class UnitFixture : IAsyncLifetime
{
    public ServiceProvider ServiceProvider { get; set; } = default!;

    protected IServiceCollection Services { get; set; }

    public UnitFixture()
    {
        var injectableTestOutputSink = new InjectableTestOutputSink();

        Services = new ServiceCollection();

        Services.AddSingleton<IInjectableTestOutputSink>(injectableTestOutputSink);
        Services.AddSingleton<SampleUtil>();

        ILogger serilogLogger = new LoggerConfiguration()
            .WriteTo.InjectableTestOutput(injectableTestOutputSink)
            .CreateLogger();

        Log.Logger = serilogLogger;

        Services.AddLogging(builder =>
        {
            builder.AddSerilog(dispose: false);
        });
    }

    public virtual Task InitializeAsync()
    {
        ServiceProvider = Services.BuildServiceProvider();

        return Task.CompletedTask;
    }

    public virtual async Task DisposeAsync()
    {
        await ServiceProvider.DisposeAsync();
    }
}

[Collection("UnitCollection")]
public class SampleUtilTests
{
    private readonly SampleUtil _util;

    public SampleUtilTests(UnitFixture fixture, ITestOutputHelper testOutputHelper)
    {
        var outputSink = (IInjectableTestOutputSink)fixture.ServiceProvider.GetService(typeof(IInjectableTestOutputSink));
        outputSink.Inject(testOutputHelper);

        _util = (SampleUtil)fixture.ServiceProvider.GetService(typeof(SampleUtil));
    }

    [Fact]
    public void DoWork_should_result_with_log_messages()
    {
        _util.DoWork();
    }
}

SampleUtil


public class SampleUtil
{
    private readonly ILogger<SampleUtil> _logger;

    public SampleUtil(ILogger<SampleUtil> logger)
    {
        _logger = logger;
    }

    public void DoWork()
    {
        Log.Logger.Information("----- Did some work (Serilog logger) -----");
        _logger.LogInformation("----- Did some work (Microsoft logger) -----");
    }
}
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 (4)

Showing the top 4 NuGet packages that depend on Serilog.Sinks.XUnit.Injectable:

Package Downloads
Soenneker.Tests.Unit

A base test providing faker and autofaker capabilities

Soenneker.Fixtures.Unit

A base xUnit fixture providing injectable log output, DI mechanisms like IServiceCollection and ServiceProvider, and AutoFaker/Faker for generating test data.

Soenneker.Fixtures.Integration

Provides a reusable and generic integration test xunit fixture that dynamically registers and configures WebApplicationFactory instances for multiple ASP.NET Core projects with support for custom app settings, authentication, logging, and test utilities.

DotCart.Drivers.Serilog

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.181 0 1/21/2026
4.0.180 10,243 1/18/2026
4.0.177 44,439 1/6/2026
4.0.176 391 1/6/2026
4.0.174 4,981 1/6/2026
4.0.173 9,764 1/3/2026
4.0.172 3,139 1/3/2026
4.0.170 4,360 1/2/2026
4.0.169 109 1/2/2026
4.0.168 6,415 1/2/2026
4.0.167 9,502 12/31/2025
4.0.166 2,357 12/31/2025
4.0.165 8,351 12/21/2025
4.0.164 3,304 12/21/2025
4.0.163 1,472 12/20/2025
4.0.162 2,976 12/20/2025
4.0.161 2,946 12/19/2025
4.0.160 2,946 12/19/2025
4.0.159 242 12/19/2025
4.0.158 367 12/17/2025
4.0.157 1,229 12/17/2025
4.0.156 298 12/17/2025
4.0.154 28,829 11/28/2025
4.0.152 7,504 11/21/2025
4.0.151 2,766 11/21/2025
4.0.150 444 11/21/2025
4.0.148 6,519 11/20/2025
4.0.147 30,335 11/6/2025
4.0.146 13,863 11/2/2025
4.0.145 3,807 10/30/2025
4.0.144 3,035 10/30/2025
4.0.143 3,576 10/30/2025
4.0.142 245 10/30/2025
4.0.141 4,058 10/29/2025
4.0.140 33,189 10/7/2025
4.0.139 11,117 9/28/2025
4.0.138 25,076 9/9/2025
4.0.137 1,565 9/9/2025
4.0.136 372 9/9/2025
4.0.135 16,615 9/3/2025
4.0.133 19,652 8/16/2025
4.0.132 9,163 8/11/2025
4.0.131 3,141 8/11/2025
4.0.130 22,890 7/25/2025
4.0.129 2,439 7/23/2025
4.0.124 3,070 7/18/2025
4.0.123 2,189 7/14/2025
4.0.122 240 7/14/2025
4.0.121 231 7/14/2025
4.0.120 221 7/14/2025
4.0.119 217 7/14/2025
4.0.108 3,352 7/12/2025
4.0.107 2,197 7/10/2025
4.0.106 32,449 7/3/2025
4.0.105 11,994 6/27/2025
4.0.104 27,839 6/7/2025
4.0.103 11,120 5/27/2025
4.0.102 19,677 5/22/2025
4.0.101 14,321 5/18/2025
4.0.100 20,150 5/7/2025
4.0.99 14,527 5/5/2025
4.0.98 692 5/5/2025
4.0.97 12,257 5/4/2025
4.0.96 12,682 4/8/2025
4.0.95 41,537 4/5/2025
4.0.94 601 4/5/2025
4.0.93 104,609 3/11/2025
4.0.92 25,484 3/1/2025
4.0.91 4,318 3/1/2025
4.0.90 58,661 2/11/2025
4.0.89 12,021 2/10/2025
4.0.88 14,429 2/7/2025
4.0.87 3,771 2/7/2025
4.0.86 3,702 2/7/2025
4.0.84 94,335 1/20/2025
4.0.83 38,836 1/14/2025
4.0.82 18,684 1/10/2025
4.0.81 1,826 1/10/2025
4.0.80 697 1/10/2025
4.0.79 16,023 1/2/2025
4.0.77 11,465 12/31/2024
4.0.76 11,473 12/31/2024
4.0.75 692 12/31/2024
4.0.74 716 12/31/2024
4.0.73 10,590 12/27/2024
4.0.72 8,588 12/24/2024
4.0.71 37,077 12/19/2024
4.0.68 5,524 12/18/2024
4.0.67 5,976 12/16/2024
4.0.66 758 12/16/2024
3.0.64 35,213 12/16/2024
3.0.63 17,925 12/9/2024
3.0.62 11,636 12/6/2024
3.0.61 10,979 12/5/2024
3.0.60 24,863 12/3/2024
3.0.59 671 12/3/2024
3.0.58 39,066 11/19/2024
3.0.57 18,840 11/14/2024
3.0.56 1,050 11/14/2024
2.1.55 6,963 11/13/2024
2.1.54 7,506 11/12/2024
2.1.53 585 11/12/2024
2.1.52 40,991 10/22/2024
2.1.51 15,793 10/11/2024
2.1.50 8,953 10/8/2024
2.1.49 1,125 10/8/2024
2.1.48 24,808 9/28/2024
2.1.47 7,615 9/27/2024
2.1.46 22,865 9/23/2024
2.1.45 46,395 9/6/2024
2.1.44 33,541 8/20/2024
2.1.43 13,263 8/13/2024
2.1.42 19,829 8/1/2024
2.1.41 5,866 7/25/2024
2.1.40 36,401 7/9/2024
2.1.39 4,982 7/9/2024
2.1.38 517 7/9/2024
2.1.37 474 7/9/2024
2.1.36 46,181 6/1/2024
2.1.35 1,347 6/1/2024
2.1.34 77,403 5/25/2024
2.1.33 502 5/25/2024
2.1.32 16,859 5/22/2024
2.1.31 13,299 5/14/2024
2.1.30 19,095 4/27/2024
2.1.29 258 4/27/2024
2.1.28 14,897 4/12/2024
2.1.27 2,989 4/11/2024
2.1.26 7,370 4/9/2024
2.1.25 23,991 3/13/2024
2.1.24 2,982 3/12/2024
2.1.23 38,438 2/21/2024
2.1.22 10,746 2/16/2024
2.1.21 295 2/16/2024
2.1.20 7,192 2/13/2024
2.1.19 22,103 2/6/2024
2.1.18 43,215 1/19/2024
2.1.17 8,037 1/15/2024
2.1.16 7,566 1/9/2024
2.1.15 5,622 1/5/2024
2.1.14 5,822 12/27/2023
2.1.13 969 12/27/2023
2.1.12 3,561 12/25/2023
2.1.11 2,876 12/24/2023
2.1.10 2,877 12/23/2023
2.1.9 299 12/23/2023
2.1.8 12,171 12/9/2023
2.1.7 278 12/9/2023
2.1.6 8,867 12/4/2023
2.1.5 7,738 11/23/2023
2.1.4 230 11/23/2023
2.1.3 2,847 11/23/2023
2.1.2 12,810 11/16/2023
2.0.52 10,059 11/15/2023
2.0.51 245 11/15/2023
2.0.50 487 11/15/2023
1.0.65 318 11/15/2023
1.0.64 7,927 11/13/2023
1.0.63 2,121 11/10/2023
1.0.62 485 11/9/2023
1.0.61 3,902 11/3/2023
1.0.60 3,261 11/1/2023
1.0.59 2,924 10/25/2023
1.0.58 3,248 10/17/2023
1.0.57 2,448 10/13/2023
1.0.56 317 10/13/2023
1.0.55 2,837 10/11/2023
1.0.54 4,982 9/18/2023
1.0.53 245 9/18/2023
1.0.52 3,330 9/13/2023
1.0.51 3,805 8/30/2023
1.0.50 7,424 8/17/2023
1.0.49 3,475 8/9/2023
1.0.48 3,177 8/7/2023
1.0.47 4,928 7/12/2023
1.0.46 2,465 7/7/2023
1.0.45 307 7/7/2023
1.0.44 4,559 6/28/2023
1.0.43 3,330 6/23/2023
1.0.42 1,168 6/22/2023
1.0.41 1,690 6/20/2023
1.0.40 2,325 6/14/2023
1.0.39 3,562 6/7/2023
1.0.38 3,715 6/2/2023
1.0.37 8,208 5/24/2023
1.0.36 946 5/23/2023
1.0.35 1,850 5/23/2023
1.0.34 1,003 5/23/2023
1.0.33 294 5/23/2023
1.0.32 649 5/22/2023
1.0.31 2,091 5/17/2023
1.0.30 1,680 5/11/2023
1.0.29 1,509 4/21/2023
1.0.28 2,384 4/12/2023
1.0.27 1,355 4/3/2023
1.0.26 1,653 3/23/2023
1.0.16 11,889 3/9/2023
1.0.15 989 2/23/2023
1.0.10 2,763 1/19/2023
1.0.9 497 1/16/2023
1.0.6 4,794 6/5/2022
1.0.5 575 6/4/2022
1.0.4 546 6/4/2022
1.0.3 553 6/4/2022
1.0.0 752 1/16/2023