Maris.Logging.Testing 1.0.0

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

Maris.Logging.Testing

Apache License v2 Maris.Logging.Testing Release date

日本語版

This library provides Microsoft.Extensions.Logging.ILogger and Microsoft.Extensions.Logging.ILogger<TCategoryName> instances which can be used in xUnit tests. You can check the log output from test classes on Visual Studio Test Explorer. This library also provides the functionality to verify the log output within the test code by integrating Microsoft.Extensions.Logging.Testing.FakeLogger.

Install

Run the following command in Package Manager Console or Command Prompt to install Maris.Logging.Testing.

  • Package Manager Console
Install-Package Maris.Logging.Testing
  • Command Prompt
dotnet add package Maris.Logging.Testing

Usage

Suppose you have a class that requires an ILogger<TCategoryName> instance as shown below, and need to write xUnit test code for this class.

using Microsoft.Extensions.Logging;

namespace Maris;

public class TestTarget
{
    private readonly ILogger<TestTarget> Logger;

    public TestTarget(ILogger<TestTarget> logger)
        => this.Logger = logger;
    
    // ...
}

Follow these steps to use a test logger in the test code.

  1. Install template for xUnit v3, and run dotnet new xunit3 to create a unit test project.
  2. Install Maris.Logging.Testing in the unit test project.
  3. In the test class, define a constructor with a Xunit.ITestOutputHelper parameter.
  4. Create a Maris.Logging.Testing.Xunit.TestLoggerManager instance in the constructor, and store it in a field. Pass an ITestOutputHelper instance to the constructor of TestLoggerManager.
  5. Call CreateLogger method of TestLoggerManager in the test method to create an ILogger or ILogger<TCategoryName> instance which can be passed to the test class.
using Maris.Logging.Testing.Xunit;
using Xunit;

namespace Maris.Tests;

public class TestClass1
{
    private readonly TestLoggerManager loggerManager;

    public TestClass1(ITestOutputHelper testOutputHelper)
        => this.loggerManager = new TestLoggerManager(testOutputHelper);

    [Fact]
    public void TestMethod()
    {
        // Creates ILogger<T> instance
        var logger = this.loggerManager.CreateLogger<TestTarget>();
        var target = new TestTarget(logger);

        // Do something...

        // TestLoggerManager.LogCollector exposes FakeLogger.Collector
        Assert.Equal(1, this.loggerManager.LogCollector.Count);  
        Assert.Equal("expectedCategory", this.loggerManager.LogCollector.LatestRecord.Category);
        Assert.Equal("expectedMessage", this.loggerManager.LogCollector.LatestRecord.Message);
    }
}

When testing Microsoft.Extensions.Hosting.IHost, AddTestLogging method can be used to register a test logger in the DI container. AddTestLogging method is defined in Microsoft.Extensions.DependencyInjection.TestLoggerServiceCollectionExtensions class. Here is an example of xUnit test code.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Maris.Logging.Testing.Xunit;
using Xunit;

namespace Maris.Tests;

public class TestClass2
{
    private readonly TestLoggerManager loggerManager;

    public TestClass2(ITestOutputHelper testOutputHelper)
        => this.loggerManager = new TestLoggerManager(testOutputHelper);

    [Fact]
    public async Task TestMethod()
    {
        using var app = this.CreateHost();
        var cancellationToken = TestContext.Current.CancellationToken;
        await app.RunAsync(cancellationToken);
        
        // Do something...
    }

    private IHost CreateHost()
    {
        var builder = Host.CreateDefaultBuilder();
        builder.ConfigureServices((context, services) =>
        {
            // Register a test logger
            services.AddTestLogging(this.loggerManager);

            // Do something...            
        });

        return builder.Build();
    }
}

The log output during the test execution is displayed in Test Explorer as follows.

test-explorer-log

For more details, see Integration tests in ASP.NET Core.

Dependencies

License

Apache License Version 2.0

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

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 428 3/31/2025
1.0.0-Beta5 130 3/31/2025
1.0.0-Beta4 131 3/31/2025
1.0.0-Beta3 444 3/26/2025
1.0.0-Beta2 440 3/26/2025