Serilog.Sinks.XUnit.Injectable 4.0.104

dotnet add package Serilog.Sinks.XUnit.Injectable --version 4.0.104
                    
NuGet\Install-Package Serilog.Sinks.XUnit.Injectable -Version 4.0.104
                    
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.104" />
                    
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.104" />
                    
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.104
                    
#r "nuget: Serilog.Sinks.XUnit.Injectable, 4.0.104"
                    
#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.
#addin nuget:?package=Serilog.Sinks.XUnit.Injectable&version=4.0.104
                    
Install Serilog.Sinks.XUnit.Injectable as a Cake Addin
#tool nuget:?package=Serilog.Sinks.XUnit.Injectable&version=4.0.104
                    
Install Serilog.Sinks.XUnit.Injectable 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: true);
        });
    }

    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 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 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. 
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.104 0 6/7/2025
4.0.103 10,157 5/27/2025
4.0.102 18,904 5/22/2025
4.0.101 12,738 5/18/2025
4.0.100 17,544 5/7/2025
4.0.99 14,144 5/5/2025
4.0.98 475 5/5/2025
4.0.97 11,940 5/4/2025
4.0.96 11,945 4/8/2025
4.0.95 40,303 4/5/2025
4.0.94 481 4/5/2025
4.0.93 83,012 3/11/2025
4.0.92 25,104 3/1/2025
4.0.91 4,101 3/1/2025
4.0.90 58,009 2/11/2025
4.0.89 11,744 2/10/2025
4.0.88 14,145 2/7/2025
4.0.87 3,609 2/7/2025
4.0.86 3,513 2/7/2025
4.0.84 93,313 1/20/2025
4.0.83 38,059 1/14/2025
4.0.82 18,324 1/10/2025
4.0.81 1,655 1/10/2025
4.0.80 566 1/10/2025
4.0.79 15,704 1/2/2025
4.0.77 11,169 12/31/2024
4.0.76 11,130 12/31/2024
4.0.75 584 12/31/2024
4.0.74 568 12/31/2024
4.0.73 10,353 12/27/2024
4.0.72 8,346 12/24/2024
4.0.71 36,341 12/19/2024
4.0.68 5,303 12/18/2024
4.0.67 5,733 12/16/2024
4.0.66 583 12/16/2024
3.0.64 15,570 12/16/2024
3.0.63 15,238 12/9/2024
3.0.62 11,301 12/6/2024
3.0.61 9,859 12/5/2024
3.0.60 24,331 12/3/2024
3.0.59 532 12/3/2024
3.0.58 36,072 11/19/2024
3.0.57 18,258 11/14/2024
3.0.56 488 11/14/2024
2.1.55 4,679 11/13/2024
2.1.54 7,256 11/12/2024
2.1.53 442 11/12/2024
2.1.52 38,744 10/22/2024
2.1.51 15,235 10/11/2024
2.1.50 8,676 10/8/2024
2.1.49 970 10/8/2024
2.1.48 23,953 9/28/2024
2.1.47 7,382 9/27/2024
2.1.46 22,362 9/23/2024
2.1.45 45,153 9/6/2024
2.1.44 32,109 8/20/2024
2.1.43 12,952 8/13/2024
2.1.42 16,274 8/1/2024
2.1.41 5,638 7/25/2024
2.1.40 35,072 7/9/2024
2.1.39 4,781 7/9/2024
2.1.38 373 7/9/2024
2.1.37 362 7/9/2024
2.1.36 44,965 6/1/2024
2.1.35 1,193 6/1/2024
2.1.34 56,669 5/25/2024
2.1.33 387 5/25/2024
2.1.32 16,266 5/22/2024
2.1.31 12,830 5/14/2024
2.1.30 18,085 4/27/2024
2.1.29 153 4/27/2024
2.1.28 14,084 4/12/2024
2.1.27 2,773 4/11/2024
2.1.26 5,850 4/9/2024
2.1.25 21,412 3/13/2024
2.1.24 2,719 3/12/2024
2.1.23 36,280 2/21/2024
2.1.22 10,238 2/16/2024
2.1.21 151 2/16/2024
2.1.20 6,835 2/13/2024
2.1.19 21,378 2/6/2024
2.1.18 41,845 1/19/2024
2.1.17 7,798 1/15/2024
2.1.16 7,193 1/9/2024
2.1.15 5,332 1/5/2024
2.1.14 5,519 12/27/2023
2.1.13 808 12/27/2023
2.1.12 3,285 12/25/2023
2.1.11 2,633 12/24/2023
2.1.10 2,660 12/23/2023
2.1.9 154 12/23/2023
2.1.8 11,462 12/9/2023
2.1.7 135 12/9/2023
2.1.6 8,246 12/4/2023
2.1.5 7,494 11/23/2023
2.1.4 150 11/23/2023
2.1.3 2,745 11/23/2023
2.1.2 12,091 11/16/2023
2.0.52 7,996 11/15/2023
2.0.51 139 11/15/2023
2.0.50 345 11/15/2023
1.0.65 204 11/15/2023
1.0.64 7,386 11/13/2023
1.0.63 1,929 11/10/2023
1.0.62 345 11/9/2023
1.0.61 3,643 11/3/2023
1.0.60 3,034 11/1/2023
1.0.59 2,724 10/25/2023
1.0.58 2,999 10/17/2023
1.0.57 2,252 10/13/2023
1.0.56 195 10/13/2023
1.0.55 2,485 10/11/2023
1.0.54 4,766 9/18/2023
1.0.53 138 9/18/2023
1.0.52 3,034 9/13/2023
1.0.51 3,592 8/30/2023
1.0.50 6,824 8/17/2023
1.0.49 3,191 8/9/2023
1.0.48 2,950 8/7/2023
1.0.47 4,470 7/12/2023
1.0.46 2,224 7/7/2023
1.0.45 181 7/7/2023
1.0.44 4,163 6/28/2023
1.0.43 3,017 6/23/2023
1.0.42 1,017 6/22/2023
1.0.41 1,508 6/20/2023
1.0.40 1,986 6/14/2023
1.0.39 3,264 6/7/2023
1.0.38 3,424 6/2/2023
1.0.37 7,806 5/24/2023
1.0.36 724 5/23/2023
1.0.35 1,518 5/23/2023
1.0.34 862 5/23/2023
1.0.33 168 5/23/2023
1.0.32 492 5/22/2023
1.0.31 1,878 5/17/2023
1.0.30 1,369 5/11/2023
1.0.29 1,274 4/21/2023
1.0.28 1,989 4/12/2023
1.0.27 1,117 4/3/2023
1.0.26 1,314 3/23/2023
1.0.16 9,428 3/9/2023
1.0.15 719 2/23/2023
1.0.10 1,946 1/19/2023
1.0.9 371 1/16/2023
1.0.6 3,667 6/5/2022
1.0.5 441 6/4/2022
1.0.4 429 6/4/2022
1.0.3 436 6/4/2022
1.0.0 547 1/16/2023