Divergic.Logging.Xunit 1.0.0

.NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
dotnet add package Divergic.Logging.Xunit --version 1.0.0
NuGet\Install-Package Divergic.Logging.Xunit -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="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
#r "nuget: Divergic.Logging.Xunit, 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.
// Install Divergic.Logging.Xunit as a Cake Addin
#addin nuget:?package=Divergic.Logging.Xunit&version=1.0.0

// Install Divergic.Logging.Xunit as a Cake Tool
#tool nuget:?package=Divergic.Logging.Xunit&version=1.0.0

Introduction

Divergic.Logging.Xunit is a NuGet package that returns an ILogger or ILogger<T> 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<T> is there using the BuildLoggerFor<T> 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<T> extension methods support this by returning a ICacheLogger or ICacheLogger<T> 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<T> 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<T>.

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");
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

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

Package Downloads
AutoMoqFixture

Package Description

U2U.EntityFrameworkCore.Testing

Package Description

Reductech.Sequence.Core.TestHarness

Class library for automatically testing Sequence® Core steps.

InsonusK.Xunit.ExpectationsTest

Base class of Xunit test expectation methods support

HerrGeneral.Test.Extension

Helper methods for HerrGeneral testing

GitHub repositories (8)

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

Repository Stars
JasperFx/marten
.NET Transactional Document DB and Event Store on PostgreSQL
asynkron/protoactor-dotnet
Proto Actor - Ultra fast distributed actors for Go, C# and Java/Kotlin
imperugo/StackExchange.Redis.Extensions
Azure/Industrial-IoT
Azure Industrial IoT Platform
sethreno/schemazen
Script and create SQL Server objects quickly
Version Downloads Last updated
4.2.0 884,632 8/9/2022
4.2.0-beta0001 116 8/9/2022
4.1.0 105,003 6/23/2022
4.1.0-beta0002 125 6/23/2022
4.1.0-beta0001 118 6/22/2022
4.0.0 374,594 1/10/2022
3.6.1-beta0010 152 1/10/2022
3.6.0 1,011,178 11/13/2020
3.5.2-beta0019 285 11/13/2020
3.5.2-beta0018 277 11/13/2020
3.5.2-beta0017 273 11/13/2020
3.5.2-beta0016 283 11/13/2020
3.5.2-beta0015 321 11/12/2020
3.5.2-beta0010 285 11/12/2020
3.5.2-beta0006 320 11/10/2020
3.5.2-beta0003 695 10/14/2020
3.5.2-beta0001 11,282 10/12/2020
3.5.1 126,367 10/8/2020
3.5.1-releasejob0001 479 8/4/2020
3.5.1-beta0032 367 10/8/2020
3.5.1-beta0031 307 10/8/2020
3.5.1-beta0030 321 10/8/2020
3.5.1-beta0029 391 9/27/2020
3.5.1-beta0028 340 9/15/2020
3.5.1-beta0027 332 9/9/2020
3.5.1-beta0026 324 9/9/2020
3.5.1-beta0025 329 9/2/2020
3.5.1-beta0024 318 8/21/2020
3.5.1-beta0023 364 8/15/2020
3.5.1-beta0022 371 8/12/2020
3.5.1-beta0021 310 8/12/2020
3.5.1-beta0020 338 8/11/2020
3.5.1-beta0019 366 8/8/2020
3.5.1-beta0018 372 8/8/2020
3.5.1-beta0017 379 8/8/2020
3.5.1-beta0015 349 8/5/2020
3.5.1-beta0013 323 8/4/2020
3.5.1-beta0012 327 8/4/2020
3.5.1-beta0011 336 8/4/2020
3.5.1-beta0010 344 8/2/2020
3.5.1-beta0009 377 7/31/2020
3.5.1-beta0008 376 7/31/2020
3.5.1-beta0007 384 7/31/2020
3.5.1-beta0003 385 7/31/2020
3.5.0 160,394 6/22/2020
3.5.0-beta0001 349 6/17/2020
3.4.0 99,383 4/28/2020
3.3.1-beta0004 347 4/28/2020
3.3.0 60,241 3/18/2020
3.3.0-beta0005 441 3/18/2020
3.2.2-beta0003 2,724 1/12/2020
3.2.2-beta0002 410 1/12/2020
3.2.2-beta0001 405 1/12/2020
3.2.1 213,644 12/18/2019
3.2.1-beta0001 425 12/18/2019
3.2.0 604 12/18/2019
3.2.0-beta0002 428 12/18/2019
3.1.0 191,981 9/27/2019
3.1.0-beta0002 402 9/27/2019
3.0.0 66,010 5/3/2019
2.2.0 42,594 4/18/2019
2.2.0-beta0001 453 4/18/2019
2.1.0 1,736 4/14/2019
2.1.0-beta0003 464 4/14/2019
2.1.0-beta0002 436 4/13/2019
2.0.1-beta0001 489 3/25/2019
2.0.0 45,853 3/19/2019
1.1.0 49,785 6/3/2018
1.0.0 8,999 6/1/2018
0.2.0-beta0047 334 8/5/2020
0.2.0-beta0045 323 8/4/2020
0.2.0-beta0036 328 7/31/2020
0.2.0-beta0035 337 7/31/2020
0.2.0-beta0034 384 6/22/2020