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
<PackageReference Include="Serilog.Sinks.XUnit3" Version="1.2.0" />
<PackageVersion Include="Serilog.Sinks.XUnit3" Version="1.2.0" />
<PackageReference Include="Serilog.Sinks.XUnit3" />
paket add Serilog.Sinks.XUnit3 --version 1.2.0
#r "nuget: Serilog.Sinks.XUnit3, 1.2.0"
#:package Serilog.Sinks.XUnit3@1.2.0
#addin nuget:?package=Serilog.Sinks.XUnit3&version=1.2.0
#tool nuget:?package=Serilog.Sinks.XUnit3&version=1.2.0
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 | 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 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.Options (>= 9.0.9)
- Serilog (>= 4.3.0)
- xunit.v3.extensibility.core (>= 3.0.1)
-
net8.0
- Microsoft.Extensions.Options (>= 9.0.9)
- Serilog (>= 4.3.0)
- xunit.v3.extensibility.core (>= 3.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.