Divergic.Logging.Xunit 1.0.0

This library provides Microsoft.Extensions.Logging support for Xunit.

There is a newer version of this package available.
See the version list below for details.
Install-Package Divergic.Logging.Xunit -Version 1.0.0
dotnet add package Divergic.Logging.Xunit --version 1.0.0
<PackageReference Include="Divergic.Logging.Xunit" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Divergic.Logging.Xunit --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Introduction

Divergic.Logging.Xunit is a NuGet package that returns an ILogger or ILogger&lt;T&gt; provider that wraps around the ITestOutputHelper supplied by xUnit. xUnit uses this helper to write log messages to the test output of each test execution. This means that any log messages from classes being tested will end up in the xUnit test result output.

Installation

Run the following in the NuGet command line or visit the NuGet package page.

Install-Package Divergic.Logging.Xunit

Usage

The common usage of this package is to call the BuildLogger extension method on the xUnit ITestOutputHelper.

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

public class MyClass
{
    private readonly ILogger _logger;

    public MyClass(ILogger logger)
    {
        _logger = logger;
    }

    public string DoSomething()
    {
        _logger.LogInformation("Hey, we did something");

        return Guid.NewGuid().ToString();
    }
}

public class MyClassTests
{
    private readonly ITestOutputHelper _output;
    private readonly ILogger _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        _output = output;
        _logger = output.BuildLogger();
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        var actual = sut.DoSomething();

        // The xUnit test output should now include the log message from MyClass.DoSomething()

        actual.Should().NotBeNullOrWhiteSpace();
    }
}

This would output the following in the test results.

Information [0]: Hey, we did something

Support for ILogger&lt;T&gt; is there using the BuildLoggerFor&lt;T&gt; extension method.

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

public class MyClassTests
{
    private readonly ITestOutputHelper _output;
    private readonly ILogger<MyClass> _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        _output = output;
        _logger = output.BuildLoggerFor<MyClass>();
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        var actual = sut.DoSomething();

        // The xUnit test output should now include the log message from MyClass.DoSomething()

        actual.Should().NotBeNull();
    }
}

Inspection

Using this library makes it really easy to output log messages from your code as part of the test results. You may want to also inspect the log messages written as part of the test assertions as well.

The BuildLogger and BuildLoggerFor&lt;T&gt; extension methods support this by returning a ICacheLogger or ICacheLogger&lt;T&gt; respectively. The cache logger is a wrapper around the created logger and exposes all the log entries written by the test.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

public class MyClassTests
{
    private readonly ITestOutputHelper _output;
    private readonly ICacheLogger _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        _output = output;
        _logger = output.BuildLogger();
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        sut.DoSomething();
        
        _logger.Count.Should().Be(1);
        _logger.Entries.Should().HaveCount(1);
        _logger.Last.Message.Should().Be("Hey, we did something");
    }
}

Perhaps you don't want to use the xUnit ITestOutputHelper but still want to use the ICacheLogger to run assertions over log messages written by the class under test. You can do this by creating a CacheLogger of CacheLogger&lt;T&gt; directly.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Xunit;

public class MyClassTests
{
    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var logger = new CacheLogger();

        var sut = new MyClass(_logger);

        sut.DoSomething();
        
        logger.Count.Should().Be(1);
        logger.Entries.Should().HaveCount(1);
        logger.Last.Message.Should().Be("Hey, we did something");
    }
}

Configured LoggerFactory

You may have an integration or acceptance test that requires additional configuration to the log providers on ILoggerFactory while also supporting the logging out to xUnit test results. You can do this by create a factory that is already configured with xUnit support.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

public class MyClassTests
{
    private readonly ILogger _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        var factory = LogFactory.Create(output);

        // call factory.UseConsole or other provider extension method

        _logger = factory.CreateLogger(nameof(MyClassTests));
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        // The xUnit test output should now include the log message from MyClass.DoSomething()

        var actual = sut.DoSomething();

        actual.Should().NotBeNullOrWhiteSpace();
    }
}

Existing Loggers

Already have an existing logger and want the above cache support? Got you covered there too using the WithCache() method.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;

public class MyClassTests
{
    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var logger = Substitute.For<ILogger>();

        logger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);

        var cacheLogger = logger.WithCache();

        var sut = new MyClass(cacheLogger);

        sut.DoSomething();

        cacheLogger.Count.Should().Be(1);
        cacheLogger.Entries.Should().HaveCount(1);
        cacheLogger.Last.Message.Should().Be("Hey, we did something");
    }
}

The WithCache() also supports ILogger&lt;T&gt;.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;

public class MyClassTests
{
    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var logger = Substitute.For<ILogger<MyClass>>();

        logger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);

        var cacheLogger = logger.WithCache();

        var sut = new MyClass(cacheLogger);

        sut.DoSomething();

        cacheLogger.Count.Should().Be(1);
        cacheLogger.Entries.Should().HaveCount(1);
        cacheLogger.Last.Message.Should().Be("Hey, we did something");
    }
}

Introduction

Divergic.Logging.Xunit is a NuGet package that returns an ILogger or ILogger&lt;T&gt; provider that wraps around the ITestOutputHelper supplied by xUnit. xUnit uses this helper to write log messages to the test output of each test execution. This means that any log messages from classes being tested will end up in the xUnit test result output.

Installation

Run the following in the NuGet command line or visit the NuGet package page.

Install-Package Divergic.Logging.Xunit

Usage

The common usage of this package is to call the BuildLogger extension method on the xUnit ITestOutputHelper.

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

public class MyClass
{
    private readonly ILogger _logger;

    public MyClass(ILogger logger)
    {
        _logger = logger;
    }

    public string DoSomething()
    {
        _logger.LogInformation("Hey, we did something");

        return Guid.NewGuid().ToString();
    }
}

public class MyClassTests
{
    private readonly ITestOutputHelper _output;
    private readonly ILogger _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        _output = output;
        _logger = output.BuildLogger();
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        var actual = sut.DoSomething();

        // The xUnit test output should now include the log message from MyClass.DoSomething()

        actual.Should().NotBeNullOrWhiteSpace();
    }
}

This would output the following in the test results.

Information [0]: Hey, we did something

Support for ILogger&lt;T&gt; is there using the BuildLoggerFor&lt;T&gt; extension method.

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

public class MyClassTests
{
    private readonly ITestOutputHelper _output;
    private readonly ILogger<MyClass> _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        _output = output;
        _logger = output.BuildLoggerFor<MyClass>();
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        var actual = sut.DoSomething();

        // The xUnit test output should now include the log message from MyClass.DoSomething()

        actual.Should().NotBeNull();
    }
}

Inspection

Using this library makes it really easy to output log messages from your code as part of the test results. You may want to also inspect the log messages written as part of the test assertions as well.

The BuildLogger and BuildLoggerFor&lt;T&gt; extension methods support this by returning a ICacheLogger or ICacheLogger&lt;T&gt; respectively. The cache logger is a wrapper around the created logger and exposes all the log entries written by the test.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

public class MyClassTests
{
    private readonly ITestOutputHelper _output;
    private readonly ICacheLogger _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        _output = output;
        _logger = output.BuildLogger();
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        sut.DoSomething();
        
        _logger.Count.Should().Be(1);
        _logger.Entries.Should().HaveCount(1);
        _logger.Last.Message.Should().Be("Hey, we did something");
    }
}

Perhaps you don't want to use the xUnit ITestOutputHelper but still want to use the ICacheLogger to run assertions over log messages written by the class under test. You can do this by creating a CacheLogger of CacheLogger&lt;T&gt; directly.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Xunit;

public class MyClassTests
{
    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var logger = new CacheLogger();

        var sut = new MyClass(_logger);

        sut.DoSomething();
        
        logger.Count.Should().Be(1);
        logger.Entries.Should().HaveCount(1);
        logger.Last.Message.Should().Be("Hey, we did something");
    }
}

Configured LoggerFactory

You may have an integration or acceptance test that requires additional configuration to the log providers on ILoggerFactory while also supporting the logging out to xUnit test results. You can do this by create a factory that is already configured with xUnit support.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Xunit;
using Xunit.Abstractions;

public class MyClassTests
{
    private readonly ILogger _logger;

    public MyClassTests(ITestOutputHelper output)
    {
        var factory = LogFactory.Create(output);

        // call factory.UseConsole or other provider extension method

        _logger = factory.CreateLogger(nameof(MyClassTests));
    }

    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var sut = new MyClass(_logger);

        // The xUnit test output should now include the log message from MyClass.DoSomething()

        var actual = sut.DoSomething();

        actual.Should().NotBeNullOrWhiteSpace();
    }
}

Existing Loggers

Already have an existing logger and want the above cache support? Got you covered there too using the WithCache() method.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;

public class MyClassTests
{
    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var logger = Substitute.For<ILogger>();

        logger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);

        var cacheLogger = logger.WithCache();

        var sut = new MyClass(cacheLogger);

        sut.DoSomething();

        cacheLogger.Count.Should().Be(1);
        cacheLogger.Entries.Should().HaveCount(1);
        cacheLogger.Last.Message.Should().Be("Hey, we did something");
    }
}

The WithCache() also supports ILogger&lt;T&gt;.

using System;
using Divergic.Logging.Xunit;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using NSubstitute;
using Xunit;

public class MyClassTests
{
    [Fact]
    public void DoSomethingReturnsValueTest()
    {
        var logger = Substitute.For<ILogger<MyClass>>();

        logger.IsEnabled(Arg.Any<LogLevel>()).Returns(true);

        var cacheLogger = logger.WithCache();

        var sut = new MyClass(cacheLogger);

        sut.DoSomething();

        cacheLogger.Count.Should().Be(1);
        cacheLogger.Entries.Should().HaveCount(1);
        cacheLogger.Last.Message.Should().Be("Hey, we did something");
    }
}

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Divergic.Logging.Xunit:

Package Downloads
AutoMoqFixture
Package Description
U2U.EntityFrameworkCore.Testing
Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Divergic.Logging.Xunit:

Repository Stars
AsynkronIT/protoactor-dotnet
Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
JosephWoodward/GlobalExceptionHandlerDotNet
Exception handling as a convention in the ASP.NET Core request pipeline

Version History

Version Downloads Last updated
3.6.0 13,110 11/13/2020
3.5.2-beta0019 50 11/13/2020
3.5.2-beta0018 52 11/13/2020
3.5.2-beta0017 49 11/13/2020
3.5.2-beta0016 52 11/13/2020
3.5.2-beta0015 93 11/12/2020
3.5.2-beta0010 55 11/12/2020
3.5.2-beta0006 81 11/10/2020
3.5.2-beta0003 346 10/14/2020
3.5.2-beta0001 917 10/12/2020
3.5.1 20,410 10/8/2020
3.5.1-releasejob0001 140 8/4/2020
3.5.1-beta0032 141 10/8/2020
3.5.1-beta0031 78 10/8/2020
3.5.1-beta0030 94 10/8/2020
3.5.1-beta0029 163 9/27/2020
3.5.1-beta0028 91 9/15/2020
3.5.1-beta0027 94 9/9/2020
3.5.1-beta0026 93 9/9/2020
3.5.1-beta0025 77 9/2/2020
3.5.1-beta0024 83 8/21/2020
3.5.1-beta0023 103 8/15/2020
3.5.1-beta0022 133 8/12/2020
3.5.1-beta0021 86 8/12/2020
3.5.1-beta0020 99 8/11/2020
3.5.1-beta0019 134 8/8/2020
3.5.1-beta0018 139 8/8/2020
3.5.1-beta0017 148 8/8/2020
3.5.1-beta0015 113 8/5/2020
3.5.1-beta0013 87 8/4/2020
3.5.1-beta0012 92 8/4/2020
3.5.1-beta0011 102 8/4/2020
3.5.1-beta0010 103 8/2/2020
3.5.1-beta0009 135 7/31/2020
3.5.1-beta0008 143 7/31/2020
3.5.1-beta0007 152 7/31/2020
3.5.1-beta0003 149 7/31/2020
3.5.0 52,136 6/22/2020
3.5.0-beta0001 129 6/17/2020
3.4.0 42,337 4/28/2020
3.3.1-beta0004 131 4/28/2020
3.3.0 29,288 3/18/2020
3.3.0-beta0005 227 3/18/2020
3.2.2-beta0003 1,828 1/12/2020
3.2.2-beta0002 184 1/12/2020
3.2.2-beta0001 174 1/12/2020
3.2.1 163,214 12/18/2019
3.2.1-beta0001 167 12/18/2019
3.2.0 202 12/18/2019
3.2.0-beta0002 190 12/18/2019
3.1.0 161,092 9/27/2019
3.1.0-beta0002 177 9/27/2019
3.0.0 45,875 5/3/2019
2.2.0 26,541 4/18/2019
2.2.0-beta0001 207 4/18/2019
2.1.0 1,310 4/14/2019
2.1.0-beta0003 228 4/14/2019
2.1.0-beta0002 196 4/13/2019
2.0.1-beta0001 234 3/25/2019
2.0.0 23,122 3/19/2019
1.1.0 36,892 6/3/2018
1.0.0 6,738 6/1/2018
0.2.0-beta0047 116 8/5/2020
0.2.0-beta0045 92 8/4/2020
0.2.0-beta0036 104 7/31/2020
0.2.0-beta0035 95 7/31/2020
0.2.0-beta0034 157 6/22/2020
Show less