Serilog.Sinks.XUnit.Injectable
3.0.56
See the version list below for details.
dotnet add package Serilog.Sinks.XUnit.Injectable --version 3.0.56
NuGet\Install-Package Serilog.Sinks.XUnit.Injectable -Version 3.0.56
<PackageReference Include="Serilog.Sinks.XUnit.Injectable" Version="3.0.56" />
paket add Serilog.Sinks.XUnit.Injectable --version 3.0.56
#r "nuget: Serilog.Sinks.XUnit.Injectable, 3.0.56"
// Install Serilog.Sinks.XUnit.Injectable as a Cake Addin #addin nuget:?package=Serilog.Sinks.XUnit.Injectable&version=3.0.56 // Install Serilog.Sinks.XUnit.Injectable as a Cake Tool #tool nuget:?package=Serilog.Sinks.XUnit.Injectable&version=3.0.56
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.
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 | Versions 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 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Serilog (>= 4.1.0)
- xunit.abstractions (>= 2.0.3)
- xunit.core (>= 2.9.2)
NuGet packages (3)
Showing the top 3 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. |
|
DotCart.Drivers.Serilog
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
3.0.58 | 14,136 | 11/19/2024 |
3.0.57 | 14,756 | 11/14/2024 |
3.0.56 | 80 | 11/14/2024 |
2.1.55 | 3,253 | 11/13/2024 |
2.1.54 | 6,774 | 11/12/2024 |
2.1.53 | 80 | 11/12/2024 |
2.1.52 | 35,765 | 10/22/2024 |
2.1.51 | 12,315 | 10/11/2024 |
2.1.50 | 8,323 | 10/8/2024 |
2.1.49 | 632 | 10/8/2024 |
2.1.48 | 22,900 | 9/28/2024 |
2.1.47 | 6,962 | 9/27/2024 |
2.1.46 | 19,885 | 9/23/2024 |
2.1.45 | 44,426 | 9/6/2024 |
2.1.44 | 31,546 | 8/20/2024 |
2.1.43 | 12,633 | 8/13/2024 |
2.1.42 | 11,996 | 8/1/2024 |
2.1.41 | 5,258 | 7/25/2024 |
2.1.40 | 32,810 | 7/9/2024 |
2.1.39 | 4,514 | 7/9/2024 |
2.1.38 | 102 | 7/9/2024 |
2.1.37 | 93 | 7/9/2024 |
2.1.36 | 42,519 | 6/1/2024 |
2.1.35 | 930 | 6/1/2024 |
2.1.34 | 24,325 | 5/25/2024 |
2.1.33 | 121 | 5/25/2024 |
2.1.32 | 14,789 | 5/22/2024 |
2.1.31 | 12,726 | 5/14/2024 |
2.1.30 | 17,235 | 4/27/2024 |
2.1.29 | 127 | 4/27/2024 |
2.1.28 | 13,354 | 4/12/2024 |
2.1.27 | 2,750 | 4/11/2024 |
2.1.26 | 5,024 | 4/9/2024 |
2.1.25 | 20,171 | 3/13/2024 |
2.1.24 | 2,675 | 3/12/2024 |
2.1.23 | 35,133 | 2/21/2024 |
2.1.22 | 10,211 | 2/16/2024 |
2.1.21 | 125 | 2/16/2024 |
2.1.20 | 6,805 | 2/13/2024 |
2.1.19 | 19,522 | 2/6/2024 |
2.1.18 | 40,959 | 1/19/2024 |
2.1.17 | 7,774 | 1/15/2024 |
2.1.16 | 7,162 | 1/9/2024 |
2.1.15 | 5,296 | 1/5/2024 |
2.1.14 | 5,480 | 12/27/2023 |
2.1.13 | 782 | 12/27/2023 |
2.1.12 | 3,244 | 12/25/2023 |
2.1.11 | 2,609 | 12/24/2023 |
2.1.10 | 2,632 | 12/23/2023 |
2.1.9 | 131 | 12/23/2023 |
2.1.8 | 10,913 | 12/9/2023 |
2.1.7 | 109 | 12/9/2023 |
2.1.6 | 6,834 | 12/4/2023 |
2.1.5 | 7,472 | 11/23/2023 |
2.1.4 | 134 | 11/23/2023 |
2.1.3 | 2,464 | 11/23/2023 |
2.1.2 | 11,824 | 11/16/2023 |
2.0.52 | 5,816 | 11/15/2023 |
2.0.51 | 123 | 11/15/2023 |
2.0.50 | 330 | 11/15/2023 |
1.0.65 | 189 | 11/15/2023 |
1.0.64 | 6,962 | 11/13/2023 |
1.0.63 | 1,912 | 11/10/2023 |
1.0.62 | 328 | 11/9/2023 |
1.0.61 | 3,628 | 11/3/2023 |
1.0.60 | 3,020 | 11/1/2023 |
1.0.59 | 2,683 | 10/25/2023 |
1.0.58 | 2,931 | 10/17/2023 |
1.0.57 | 2,232 | 10/13/2023 |
1.0.56 | 174 | 10/13/2023 |
1.0.55 | 2,469 | 10/11/2023 |
1.0.54 | 4,722 | 9/18/2023 |
1.0.53 | 123 | 9/18/2023 |
1.0.52 | 3,002 | 9/13/2023 |
1.0.51 | 3,574 | 8/30/2023 |
1.0.50 | 6,798 | 8/17/2023 |
1.0.49 | 3,173 | 8/9/2023 |
1.0.48 | 2,930 | 8/7/2023 |
1.0.47 | 4,448 | 7/12/2023 |
1.0.46 | 2,193 | 7/7/2023 |
1.0.45 | 161 | 7/7/2023 |
1.0.44 | 4,134 | 6/28/2023 |
1.0.43 | 2,996 | 6/23/2023 |
1.0.42 | 997 | 6/22/2023 |
1.0.41 | 1,466 | 6/20/2023 |
1.0.40 | 1,960 | 6/14/2023 |
1.0.39 | 3,221 | 6/7/2023 |
1.0.38 | 3,380 | 6/2/2023 |
1.0.37 | 7,780 | 5/24/2023 |
1.0.36 | 697 | 5/23/2023 |
1.0.35 | 1,487 | 5/23/2023 |
1.0.34 | 835 | 5/23/2023 |
1.0.33 | 141 | 5/23/2023 |
1.0.32 | 446 | 5/22/2023 |
1.0.31 | 1,851 | 5/17/2023 |
1.0.30 | 1,342 | 5/11/2023 |
1.0.29 | 1,223 | 4/21/2023 |
1.0.28 | 1,963 | 4/12/2023 |
1.0.27 | 1,087 | 4/3/2023 |
1.0.26 | 1,289 | 3/23/2023 |
1.0.16 | 8,182 | 3/9/2023 |
1.0.15 | 689 | 2/23/2023 |
1.0.10 | 1,915 | 1/19/2023 |
1.0.9 | 340 | 1/16/2023 |
1.0.6 | 3,628 | 6/5/2022 |
1.0.5 | 404 | 6/4/2022 |
1.0.4 | 393 | 6/4/2022 |
1.0.3 | 399 | 6/4/2022 |
1.0.0 | 434 | 1/16/2023 |