Logging extensions for xunit
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.azure-pipelines
.github
.vscode
src/Logging.XUnit
tests
.editorconfig
.gitattributes
.gitignore
.travis.yml
Build.ps1
CODE_OF_CONDUCT.md
CommonAssemblyInfo.cs
Directory.Build.props
Directory.Build.targets
LICENSE
Logging.XUnit.ruleset
Logging.XUnit.sln
Logging.XUnit.snk
NuGet.config
README.md
appveyor.yml
build.sh
global.json
stylecop.json

README.md

xunit Logging

NuGet

Windows Linux/OS X Linux/macOS/Windows
Build Status Windows build status Code coverage Linux build status Azure Pipelines build status
Build History Windows build history Linux build history Build history

Introduction

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

Installation

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

dotnet add package MartinCostello.Logging.XUnit

Usage

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

        [Fact]
        public void Calculator_Sums_Two_Integers()
        {
            // Arrange
            var services = new ServiceCollection()
                .AddLogging((builder) => builder.AddXUnit(OutputHelper))
                .AddSingleton<Calculator>();

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

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

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 library yourself requires Git and the .NET Core SDK to be installed (version 2.1.402 or later).

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

Windows

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

Linux/macOS

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