Skip to content
Switch branches/tags
Go to file

xunit Logging


Build status


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


To install the library from NuGet using the .NET SDK run:

dotnet add package MartinCostello.Logging.XUnit


using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

namespace MyApp.Calculator
    public class CalculatorTests
        public CalculatorTests(ITestOutputHelper outputHelper)
            OutputHelper = outputHelper;

        private ITestOutputHelper OutputHelper { get; }

        public void Calculator_Sums_Two_Integers()
            // Arrange
            var services = new ServiceCollection()
                .AddLogging((builder) => builder.AddXUnit(OutputHelper))

            var calculator = services

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

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

    public sealed class Calculator
        private readonly ILogger _logger;

        public Calculator(ILogger<Calculator> logger)
            _logger = 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:

  1. Unit tests
  2. Integration tests for an ASP.NET Core HTTP application


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


The repository is hosted in GitHub:


This project is licensed under the Apache 2.0 license.

Building and Testing

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

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

git clone
cd xunit-logging