MicrosoftExtensions.Logging.Xunit 1.1.1

dotnet add package MicrosoftExtensions.Logging.Xunit --version 1.1.1
                    
NuGet\Install-Package MicrosoftExtensions.Logging.Xunit -Version 1.1.1
                    
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="MicrosoftExtensions.Logging.Xunit" Version="1.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MicrosoftExtensions.Logging.Xunit" Version="1.1.1" />
                    
Directory.Packages.props
<PackageReference Include="MicrosoftExtensions.Logging.Xunit" />
                    
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 MicrosoftExtensions.Logging.Xunit --version 1.1.1
                    
#r "nuget: MicrosoftExtensions.Logging.Xunit, 1.1.1"
                    
#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 MicrosoftExtensions.Logging.Xunit@1.1.1
                    
#: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=MicrosoftExtensions.Logging.Xunit&version=1.1.1
                    
Install as a Cake Addin
#tool nuget:?package=MicrosoftExtensions.Logging.Xunit&version=1.1.1
                    
Install as a Cake Tool

MicrosoftExtensions.Logging.Xunit

A 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.

Features

  • Microsoft Extensions Logging compatible ILogger implementation for xUnit tests
  • Forwards log messages to xUnit's ITestOutputHelper for test output visibility
  • Multi-targeting: netstandard2.0, netstandard2.1, net462, net8.0, net9.0
  • Full log level support with formatted output
  • Exception logging with stack traces
  • Thread-safe logger implementation
  • Easy integration with existing logging infrastructure

Installation

Install from NuGet:

# .NET CLI
dotnet add package Microsoft.Extensions.Logging.Xunit

# Package Manager Console
Install-Package Microsoft.Extensions.Logging.Xunit

Quick Start

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");
        }
    }
}

Advanced Usage

Using with Dependency Injection

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
    }
}

Custom Log Formatting

The logger automatically formats messages with log level, category, and timestamp information for clear test output readability.

Example Output

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 25

License

GPL-3.0-or-later


Feedback and contributions are welcome! Open an issue or PR.

Product 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 is compatible.  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 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MicrosoftExtensions.Logging.Xunit:

Package Downloads
ChannelAdam.TestFramework.Xunit

A .NET library for using xUnit (and Moq) on top of the ChannelAdam Test Framework.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.1 863 7/16/2025
1.1.0-beta.1.g5120d81 22,327 8/17/2022
1.0.0 377,265 2/23/2018

# Release Notes
## Version 1.1.1
### Features
- **Core**: High-performance xUnit integration for Microsoft.Extensions.Logging
- **Multi-framework**: Support for .NET Standard 2.0/2.1, .NET Framework 4.6.2, .NET 8.0, and .NET 9.0
- **Thread-safe**: Thread-safe logger implementation for concurrent test execution
- **Full logging support**: Complete implementation of ILogger interface with all log levels
- **Exception handling**: Comprehensive exception logging with stack traces
- **Structured logging**: Support for message templates and structured data
- **Easy integration**: Simple setup with ITestOutputHelper for immediate test output visibility
### Core Components
- **XunitLoggerProvider**: Creates logger instances that output to xUnit test output
- **XunitLogger**: Full-featured ILogger implementation with formatted output
- **Thread-safe operations**: Safe for use in parallel test execution scenarios
- **Memory-efficient**: Optimized string building and message formatting
### API Features
- Complete ILogger interface implementation
- Support for all log levels (Trace, Debug, Information, Warning, Error, Critical)
- Structured logging with message templates
- Exception logging with full stack trace output
- Category-based logger creation (string and generic type-based)
- BeginScope support (delegates to NullLogger)
- Always-enabled logging (IsEnabled always returns true)
### Compatibility
- **Target Frameworks**: netstandard2.0, netstandard2.1, net462, net8.0, net9.0
- **Dependencies**:
- Microsoft.Extensions.Logging.Abstractions
- xunit.abstractions
- **Nullable Reference Types**: Enabled for modern .NET versions (net8.0, net9.0, netstandard2.1)
- **Documentation**: Comprehensive XML documentation for all public APIs
### Performance
- Efficient string building using ThreadStatic StringBuilder
- Minimal allocation overhead for log message formatting
- Zero-overhead when test output is not captured
- Optimized for high-frequency logging scenarios in tests
### Usage Scenarios
- Unit testing with logging output capture
- Integration testing with service dependencies
- Debugging complex test scenarios
- Capturing application logs during test execution
- Testing logging-dependent components and services
## Future Enhancements
- Enhanced formatting options and customization
- Log filtering and level configuration support
- Performance improvements for high-volume logging
- Additional output formatting options