{"id":20266327,"url":"https://github.com/soenneker/serilog-sinks-xunit-injectable","last_synced_at":"2026-06-05T03:01:37.690Z","repository":{"id":76292263,"uuid":"497626095","full_name":"soenneker/serilog-sinks-xunit-injectable","owner":"soenneker","description":"The injectable, Serilog xUnit test output sink","archived":false,"fork":false,"pushed_at":"2026-05-30T01:25:16.000Z","size":440,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-05-30T03:13:16.763Z","etag":null,"topics":["csharp","dotnet","logging","serilog","testing","xunit"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soenneker.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":"soenneker","thanks_dev":"soenneker"}},"created_at":"2022-05-29T15:13:04.000Z","updated_at":"2026-05-30T01:25:19.000Z","dependencies_parsed_at":"2023-11-23T02:36:30.041Z","dependency_job_id":"2bf192c9-cb6c-47d0-8604-2d18601e620d","html_url":"https://github.com/soenneker/serilog-sinks-xunit-injectable","commit_stats":{"total_commits":25,"total_committers":2,"mean_commits":12.5,"dds":"0.040000000000000036","last_synced_commit":"9437266395a7047d6f03616d8829b35cab236550"},"previous_names":[],"tags_count":91,"template":false,"template_full_name":null,"purl":"pkg:github/soenneker/serilog-sinks-xunit-injectable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fserilog-sinks-xunit-injectable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fserilog-sinks-xunit-injectable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fserilog-sinks-xunit-injectable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fserilog-sinks-xunit-injectable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soenneker","download_url":"https://codeload.github.com/soenneker/serilog-sinks-xunit-injectable/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soenneker%2Fserilog-sinks-xunit-injectable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33927314,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-05T02:00:06.157Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","logging","serilog","testing","xunit"],"created_at":"2024-11-14T12:08:16.656Z","updated_at":"2026-06-05T03:01:37.661Z","avatar_url":"https://github.com/soenneker.png","language":"C#","funding_links":["https://github.com/sponsors/soenneker","https://thanks.dev/soenneker"],"categories":[],"sub_categories":[],"readme":"[![](https://img.shields.io/nuget/v/Serilog.Sinks.XUnit.Injectable.svg?style=for-the-badge)](https://www.nuget.org/packages/Serilog.Sinks.XUnit.Injectable/)\n[![](https://img.shields.io/github/actions/workflow/status/soenneker/serilog-sinks-xunit-injectable/publish-package.yml?style=for-the-badge)](https://github.com/soenneker/serilog-sinks-xunit-injectable/actions/workflows/publish-package.yml)\n[![](https://img.shields.io/nuget/dt/Serilog.Sinks.XUnit.Injectable.svg?style=for-the-badge)](https://www.nuget.org/packages/Serilog.Sinks.XUnit.Injectable/)\n[![](https://img.shields.io/github/actions/workflow/status/soenneker/serilog-sinks-xunit-injectable/codeql.yml?label=CodeQL\u0026style=for-the-badge)](https://github.com/soenneker/serilog-sinks-xunit-injectable/actions/workflows/codeql.yml)\n\n# Serilog.Sinks.XUnit.Injectable\n### The injectable, Serilog xUnit test output sink\n\nLeverage xUnit's [`TestOutputHelper`](https://xunit.net/docs/capturing-output) across tests that share state\n\n### Common use cases\n- Integration tests (i.e. [`WebApplicationFactory`](https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0))\n- Unit tests that utilize Dependency Injection in a fixture\n\n## Why?\nWhen 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. \n\nxUnit 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.\n\nThis 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.\n\nExamples 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.\n\n### xUnit Compatibility\n\n- Version 3.x: Supports xUnit 2.9.\n- Latest Version: Fully supports xUnit 3.\n\n## Installation\n\n```\ndotnet add package Serilog.Sinks.XUnit.Injectable\n```\n\n---\n### Example: `WebApplicationFactory` \"integration tests\"\n---\n```csharp\npublic class ApiFixture : IAsyncLifetime\n{\n    public WebApplicationFactory\u003cProgram\u003e ApiFactory { get; set; } = default!;\n\n    public Task InitializeAsync()\n    {\n        ApiFactory = new WebApplicationFactory\u003cProgram\u003e();\n        ApiFactory = ApiFactory.WithWebHostBuilder(builder =\u003e\n        {\n            // Instantiate the sink, with any configuration (like outputTemplate, formatProvider)\n            var injectableTestOutputSink = new InjectableTestOutputSink();\n\n            builder.ConfigureServices(services =\u003e\n            {\n                // Register the sink as a singleton\n                services.AddSingleton\u003cIInjectableTestOutputSink\u003e(injectableTestOutputSink); \n            });\n\n            builder.UseSerilog((_, loggerConfiguration) =\u003e\n            {\n                // Add the sink to the logger configuration\n                loggerConfiguration.WriteTo.InjectableTestOutput(injectableTestOutputSink);\n            });\n        });\n\n        return Task.CompletedTask;\n    }\n\n    public async Task DisposeAsync()\n    {\n        await ApiFactory.DisposeAsync();\n    }\n}\n```\n\n```csharp\n[Collection(\"ApiCollection\")]\npublic class ApiTests\n{\n    private readonly HttpClient _client;\n\n    public ApiTests(ApiFixture fixture, ITestOutputHelper testOutputHelper)\n    {\n        var outputSink = (IInjectableTestOutputSink)fixture.ApiFactory.Services.GetService(typeof(IInjectableTestOutputSink))!;\n        outputSink.Inject(testOutputHelper); // \u003c-- inject the new ITestOutputHelper into the sink\n\n        _client = fixture.ApiFactory.CreateClient();\n    }\n\n    [Fact]\n    public async Task Get_should_have_log_messages_and_be_successful()\n    {\n        HttpResponseMessage response = await _client.GetAsync(\"/\");\n        response.EnsureSuccessStatusCode();\n    }\n}\n```\n\n---\n### Example: `ServiceProvider` \"Fixtured unit tests\"\n---\n```csharp\npublic class UnitFixture : IAsyncLifetime\n{\n    public ServiceProvider ServiceProvider { get; set; } = default!;\n\n    protected IServiceCollection Services { get; set; }\n\n    public UnitFixture()\n    {\n        var injectableTestOutputSink = new InjectableTestOutputSink();\n\n        Services = new ServiceCollection();\n\n        Services.AddSingleton\u003cIInjectableTestOutputSink\u003e(injectableTestOutputSink);\n        Services.AddSingleton\u003cSampleUtil\u003e();\n\n        ILogger serilogLogger = new LoggerConfiguration()\n            .WriteTo.InjectableTestOutput(injectableTestOutputSink)\n            .CreateLogger();\n\n        Log.Logger = serilogLogger;\n\n        Services.AddLogging(builder =\u003e\n        {\n            builder.AddSerilog(dispose: false);\n        });\n    }\n\n    public virtual Task InitializeAsync()\n    {\n        ServiceProvider = Services.BuildServiceProvider();\n\n        return Task.CompletedTask;\n    }\n\n    public virtual async Task DisposeAsync()\n    {\n        await ServiceProvider.DisposeAsync();\n    }\n}\n\n```\n\n```csharp\n[Collection(\"UnitCollection\")]\npublic class SampleUtilTests\n{\n    private readonly SampleUtil _util;\n\n    public SampleUtilTests(UnitFixture fixture, ITestOutputHelper testOutputHelper)\n    {\n        var outputSink = (IInjectableTestOutputSink)fixture.ServiceProvider.GetService(typeof(IInjectableTestOutputSink));\n        outputSink.Inject(testOutputHelper);\n\n        _util = (SampleUtil)fixture.ServiceProvider.GetService(typeof(SampleUtil));\n    }\n\n    [Fact]\n    public void DoWork_should_result_with_log_messages()\n    {\n        _util.DoWork();\n    }\n}\n```\n---\n### `SampleUtil`\n---\n```csharp\npublic class SampleUtil\n{\n    private readonly ILogger\u003cSampleUtil\u003e _logger;\n\n    public SampleUtil(ILogger\u003cSampleUtil\u003e logger)\n    {\n        _logger = logger;\n    }\n\n    public void DoWork()\n    {\n        Log.Logger.Information(\"----- Did some work (Serilog logger) -----\");\n        _logger.LogInformation(\"----- Did some work (Microsoft logger) -----\");\n    }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fserilog-sinks-xunit-injectable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoenneker%2Fserilog-sinks-xunit-injectable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoenneker%2Fserilog-sinks-xunit-injectable/lists"}