Meziantou.Extensions.Logging.Xunit.v3 3.0.4

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Meziantou.Extensions.Logging.Xunit.v3 --version 3.0.4
                    
NuGet\Install-Package Meziantou.Extensions.Logging.Xunit.v3 -Version 3.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Meziantou.Extensions.Logging.Xunit.v3" Version="3.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Meziantou.Extensions.Logging.Xunit.v3" Version="3.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Meziantou.Extensions.Logging.Xunit.v3" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Meziantou.Extensions.Logging.Xunit.v3 --version 3.0.4
                    
#r "nuget: Meziantou.Extensions.Logging.Xunit.v3, 3.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Meziantou.Extensions.Logging.Xunit.v3@3.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Meziantou.Extensions.Logging.Xunit.v3&version=3.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Meziantou.Extensions.Logging.Xunit.v3&version=3.0.4
                    
Install as a Cake Tool

Meziantou.Extensions.Logging.Xunit.v3

Statically create XUnitLogger or XUnitLogger<T>

ILogger logger = XUnitLogger.CreateLogger();
ILogger<MyType> logger = XUnitLogger.CreateLogger<MyType>();

These overloads do not take an ITestOutputHelper. The logger resolves TestContext.Current.TestOutputHelper each time it writes, so it always targets the test that is currently running. This is the recommended way to log from code whose lifetime is not tied to a single test, such as a host or a fixture shared by several tests.

Statically create XUnitLogger or XUnitLogger<T> by passing an existing ITestOutputHelper

ILogger logger = XUnitLogger.CreateLogger(testOutputHelper);
ILogger<MyType> logger = XUnitLogger.CreateLogger<MyType>(testOutputHelper);

A logger created this way writes to that specific helper. If the helper belongs to a test that has already finished, the log record is dropped instead of throwing.

Configure the output

Every entry point accepts an optional XUnitLoggerOptions:

ILogger logger = XUnitLogger.CreateLogger(testOutputHelper, new XUnitLoggerOptions
{
    IncludeLogLevel = true,
    IncludeCategory = true,
    IncludeScopes = true,
    TimestampFormat = "HH:mm:ss.fff",
    UseUtcTimestamp = true,
});

// 12:20:41.812 warn [MyNamespace.MyType] Something happened
//  => TheScope
Property Default Effect
IncludeLogLevel false Prefixes the record with trce, dbug, info, warn, fail or crit.
IncludeCategory false Prefixes the record with [CategoryName].
IncludeScopes false Appends the active scopes, one per line, prefixed with =>.
TimestampFormat null Date and time format string. No timestamp is written when null.
UseUtcTimestamp false Formats the timestamp in UTC instead of local time.

Register the logger with dependency injection

var host = new HostBuilder()
    .ConfigureLogging(builder =>
    {
        builder.AddXunit();
    })
    .Build();

AddXunit has four overloads: no argument, an ITestOutputHelper, an XUnitLoggerOptions, or both. Use the parameterless one to write to whichever test is running, and pass an ITestOutputHelper only when you need to target a specific one.

Because ITestOutputHelper and XUnitLoggerOptions are both optional reference types, AddXunit(null) is ambiguous and does not compile. Call AddXunit() to say "no test output helper", or name the argument (AddXunit(testOutputHelper: null)) if you are passing one through. The same applies to the XUnitLoggerProvider constructors.

Using WebApplicationFactory

If you are using a WebApplicationFactory:

public class UnitTest1(ITestOutputHelper testOutputHelper)
{
    [Fact]
    public async Task Test1()
    {
        using var factory = new WebApplicationFactory<Program>()
            .WithWebHostBuilder(builder =>
            {
                builder.ConfigureLogging(builder =>
                {
                    // You can override the logging configuration if needed
                    //builder.SetMinimumLevel(LogLevel.Trace);
                    //builder.AddFilter(_ => true);

                    // Register the xUnit logger provider
                    builder.AddXunit(testOutputHelper, new XUnitLoggerOptions { IncludeScopes = true });
                });
            });
    }
}

If the factory outlives the test that created it, prefer builder.AddXunit() without the helper so each record goes to the test that is running when it is written.

Blog post about this package: How to write logs from ILogger to xUnit.net ITestOutputHelper

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Meziantou.Extensions.Logging.Xunit.v3:

Package Downloads
TwoRivers.Testing

Framework and tools for implementing applications by following best practices in a developer friendly way. Built from commit 59db18f48398b711c50e16d5da7aacc2e0e6f014 https://github.com/TwoRiversIT/AppFramework/commit/59db18f48398b711c50e16d5da7aacc2e0e6f014

Vulthil.xUnit

Reusable xUnit base infrastructure for integration and unit test composition.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Meziantou.Extensions.Logging.Xunit.v3:

Repository Stars
kerryjiang/SuperSocket
SuperSocket is a high-performance, extensible socket server application framework for .NET. It provides a robust architecture for building custom network communication applications with support for multiple protocols including TCP, UDP, and WebSocket.
wiremock/WireMock.Net
WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on WireMock Java, but extended and different functionality. Full documentation can be found at https://wiremock.org/dotnet/.
Version Downloads Last Updated
3.0.5 137 9/3/2026
3.0.4 247 9/3/2026
3.0.3 1,134 8/29/2026
3.0.2 1,489 8/23/2026
3.0.1 8,592 8/9/2026
3.0.0 30,285 7/5/2026
2.0.3 6,703 6/28/2026
2.0.2 107,287 6/13/2026
2.0.1 23,900 5/14/2026
2.0.0 4,719 5/6/2026
1.1.27 19,590 4/25/2026
1.1.26 41,119 3/29/2026
1.1.25 38,485 3/15/2026
1.1.24 29,053 2/22/2026
1.1.23 33,262 1/25/2026
1.1.22 3,139 1/18/2026
1.1.21 8,621 12/28/2025
1.1.20 8,061 12/14/2025
1.1.19 4,204 11/30/2025
1.1.18 15,290 11/16/2025
Loading failed