An xUnit Microsoft.Extensions.Logging.ILoggerProvider
that writes logs to the test output.
Its formatting and its configuration is based on the Microsoft.Extensions.Logging.Console.SimpleConsoleFormatter
created from the ConsoleLoggerExtensions.AddSimpleConsole Method.
Install Devodo.Logging.Xunit
as a NuGet package:
Install-Package Devodo.Logging.Xunit
Or via the .NET command line interface:
dotnet add package Devodo.Logging.Xunit
Given a simple ASP.NET Minimal API as follows:
var app = WebApplication.Create(args);
app.MapGet("/echo/{message}", (string message, ILogger<Program> logger) =>
{
logger.LogInformation("Handling echo request with input: {Message}", message);
return message;
});
app.Run();
The application logs can be sent to xUnit test output as follows:
public class ExampleTests
{
private readonly ITestOutputHelper _outputHelper;
public ExampleTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}
[Fact]
public async Task GivenHttpClient_WhenGetRoot_ThenRespondsHello()
{
// ARRANGE
var httpClient = new WebApplicationFactory<Program>().WithWebHostBuilder(builder =>
{
builder.ConfigureLogging(logging =>
{
// Add an xUnit logging provider that writes to the ITestOutputHelper
logging.AddXunit(_outputHelper);
});
}).CreateClient();
// ACT
var response = await httpClient.GetAsync("/echo/hello xunit logging");
// ASSERT
Assert.Equal("hello xunit logging", await response.Content.ReadAsStringAsync());
}
}
The output from the test above is:
info: Program[0]
Handling echo request with input: hello xunit logging
The following format options can be configured:
logging.AddXunit(_outputHelper, options =>
{
options.IncludeScopes = true;
options.TimestampFormat = "HH:mm:ss ";
options.SingleLine = false;
options.UseUtcTimestamp = true;
});