Xunit integration for Microsoft.Extensions.Logging. Provides ILogger implementation that forwards log messages to Xunit's ITestOutputHelper for easy debugging and test output visibility.
Deps
10
Install Size
—
Vulns
✓ 0
Published
Aug 22, 2025
$ dotnet add package MicrosoftExtensions.Logging.XunitA high-performance xUnit integration library for Microsoft.Extensions.Logging, enabling seamless log output capture in unit tests. Compatible with .NET Framework 4.6.2+, .NET Core 2.0+, .NET 6/8/9+, and modern .NET platforms.
ILogger implementation for xUnit testsITestOutputHelper for test output visibilitynetstandard2.0, netstandard2.1, net462, net8.0, net9.0Install from NuGet:
# .NET CLI
dotnet add package Microsoft.Extensions.Logging.Xunit
# Package Manager Console
Install-Package Microsoft.Extensions.Logging.Xunit
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Xunit;
using Xunit;
using Xunit.Abstractions;
public class UnitTest1
{
private readonly ITestOutputHelper testOutputHelper;
private readonly ILoggerFactory loggerFactory;
public UnitTest1(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
this.loggerFactory = new LoggerFactory(new[] { new XunitLoggerProvider(testOutputHelper) });
}
[Fact]
public void Test1()
{
var logger = loggerFactory.CreateLogger("Test1");
logger.LogInformation("Hello World!");
var typedLogger = loggerFactory.CreateLogger<UnitTest1>();
typedLogger.LogInformation("Hello from typed logger!");
// Log with structured data
logger.LogWarning("Processing {ItemCount} items", 42);
// Log exceptions
try
{
throw new InvalidOperationException("Test exception");
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred during processing");
}
}
}
public class ServiceTest
{
private readonly ITestOutputHelper testOutputHelper;
private readonly ServiceProvider serviceProvider;
public ServiceTest(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
var services = new ServiceCollection();
services.AddLogging(builder =>
{
builder.AddProvider(new XunitLoggerProvider(testOutputHelper));
});
services.AddTransient<MyService>();
this.serviceProvider = services.BuildServiceProvider();
}
[Fact]
public void TestService()
{
var service = serviceProvider.GetRequiredService<MyService>();
service.DoWork(); // Logs will appear in test output
}
}The logger automatically formats messages with log level, category, and timestamp information for clear test output readability.
When running tests, log messages appear in the test output:
info: Test1[0]
Hello World!
warn: Test1[0]
Processing 42 items
fail: Test1[0]
An error occurred during processing
System.InvalidOperationException: Test exception
at UnitTest1.Test1() in C:\Example\UnitTest1.cs:line 25LGPL-3.0-or-later WITH LGPL-3.0-linking-exception
Feedback and contributions are welcome! Open an issue or PR.