Skip to content

Latest commit

 

History

History
151 lines (111 loc) · 6.73 KB

README.md

File metadata and controls

151 lines (111 loc) · 6.73 KB

xunit Logging

Build status codecov OpenSSF Scorecard

xunit version Package NuGet Version
xunit v2 MartinCostello.Logging.XUnit NuGet NuGet Downloads
xunit v3 MartinCostello.Logging.XUnit.v3 NuGet NuGet Downloads

Introduction

MartinCostello.Logging.XUnit and MartinCostello.Logging.XUnit.v3 provide extensions to hook into the ILogger infrastructure to output logs from your xunit tests to the test output.

Projects using xunit v2 should use the MartinCostello.Logging.XUnit package, while projects using xunit v3 should use the MartinCostello.Logging.XUnit.v3 package.

Note

This library is designed for the Microsoft logging implementation of ILoggerFactory. For other logging implementations, such as Serilog, consider using packages such as Serilog.Sinks.XUnit instead.

Installation

To install the library from NuGet using the .NET SDK run one of the following commands.

For xunit v2

dotnet add package MartinCostello.Logging.XUnit

For xunit v3

dotnet add package MartinCostello.Logging.XUnit.v3

Usage

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions; // For xunit v2 - not required for xunit v3

namespace MyApp.Calculator;

public class CalculatorTests(ITestOutputHelper outputHelper)
{
    [Fact]
    public void Calculator_Sums_Two_Integers()
    {
        // Arrange
        using var serviceProvider = new ServiceCollection()
            .AddLogging((builder) => builder.AddXUnit(outputHelper))
            .AddSingleton<Calculator>()
            .BuildServiceProvider();

        var calculator = services.GetRequiredService<Calculator>();

        // Act
        int actual = calculator.Sum(1, 2);

        // Assert
        Assert.AreEqual(3, actual);
    }
}

public sealed class Calculator(ILogger<Calculator> logger)
{
    public int Sum(int x, int y)
    {
        int sum = x + y;

        logger.LogInformation("The sum of {x} and {y} is {sum}.", x, y, sum);

        return sum;
    }
}

See below for links to more examples:

Migrating to xunit v3

Xunit v3 contains many major architectural changes which means the same package that supports logging for xunit v2 cannot be used with xunit v3. The equivalent NuGet package to support logging for xunit v3 is the new MartinCostello.Logging.XUnit.v3 package.

To migrate usage of MartinCostello.Logging.XUnit to MartinCostello.Logging.XUnit.v3 for xunit v3:

  1. Follow the relevant steps to migrate any test projects from xunit v2 to v3.

    • The most relevant change in xunit v3 is that the ITestOutputHelper type has moved from the Xunit.Abstractions namespace to Xunit.
  2. Change any package references from MartinCostello.Logging.XUnit to MartinCostello.Logging.XUnit.v3.

    - <PackageReference Include="MartinCostello.Logging.XUnit" Version="0.5.0" />
    + <PackageReference Include="MartinCostello.Logging.XUnit.v3" Version="0.5.0" />

Feedback

Any feedback or issues can be added to the issues for this project in GitHub.

Repository

The repository is hosted in GitHub: https://github.com/martincostello/xunit-logging.git

License

This project is licensed under the Apache 2.0 license.

Building and Testing

Compiling the solution yourself requires Git and the .NET SDK to be installed (version 9.0.100 or later).

To build and test the solution locally from a terminal/command-line, run the following set of commands:

git clone https://github.com/martincostello/xunit-logging.git
cd xunit-logging
./build.ps1