Divergic.Logging.Xunit
1.0.0
This package has been renamed to Neovolve.Logging.Xunit
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
<PackageReference Include="Divergic.Logging.Xunit" Version="1.0.0" />
<PackageVersion Include="Divergic.Logging.Xunit" Version="1.0.0" />
<PackageReference Include="Divergic.Logging.Xunit" />
paket add Divergic.Logging.Xunit --version 1.0.0
#r "nuget: Divergic.Logging.Xunit, 1.0.0"
#:package Divergic.Logging.Xunit@1.0.0
#addin nuget:?package=Divergic.Logging.Xunit&version=1.0.0
#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 | Versions 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. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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. |
-
.NETStandard 2.0
- Ensure.That (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 2.1.0)
- Xunit.Abstractions (>= 2.0.1)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on Divergic.Logging.Xunit:
| Package | Downloads |
|---|---|
|
AutoMoqFixture
Package Description |
|
|
InsonusK.Xunit.ExpectationsTest
Base class of Xunit test expectation methods support |
|
|
HerrGeneral.Test.Extension
Helper methods for HerrGeneral testing |
|
|
MicroPlumberd.Testing
Testing utilities for MicroPlumberd. Includes Given-When-Then specs, Docker-based EventStore server management, and integration testing helpers for aggregates, command handlers, and process managers. |
|
|
U2U.EntityFrameworkCore.Testing
Package Description |
GitHub repositories (5)
Showing the top 5 popular GitHub repositories that depend on Divergic.Logging.Xunit:
| Repository | Stars |
|---|---|
|
sethreno/schemazen
Script and create SQL Server objects quickly
|
|
|
josephwoodward/GlobalExceptionHandlerDotNet
Exception handling as a convention in the ASP.NET Core request pipeline
|
|
|
17MKH/Mkh
基于.Net 6+Vue3开发的业务模块化快速开发框架
|
|
|
TrakHound/MTConnect.NET
Fully featured .NET library in C# to build MTConnect Agent, Adapter, and Client Applications. Pre-built Agents with Windows Installers. Support for Windows and Linux. Supports MTConnect Versions up to 2.7. Supports .NET Framework 4.6.1 through .NET 10
|
|
|
microsoft/botframework-components
The repository for components built by Microsoft for the Azure Bot Framework.
|
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 4.3.1 | 1,561,012 | 4/20/2024 | |
| 4.3.0 | 844,383 | 11/21/2023 | |
| 4.2.0 | 2,991,114 | 8/9/2022 | |
| 4.2.0-beta0001 | 432 | 8/9/2022 | |
| 4.1.0 | 212,398 | 6/23/2022 | |
| 4.1.0-beta0002 | 427 | 6/23/2022 | |
| 4.1.0-beta0001 | 440 | 6/22/2022 | |
| 4.0.0 | 662,479 | 1/10/2022 | |
| 3.6.1-beta0010 | 548 | 1/10/2022 | |
| 3.6.0 | 1,397,925 | 11/13/2020 | |
| 3.5.2-beta0019 | 652 | 11/13/2020 | |
| 3.5.2-beta0018 | 619 | 11/13/2020 | |
| 3.5.2-beta0017 | 610 | 11/13/2020 | |
| 3.5.2-beta0016 | 613 | 11/13/2020 | |
| 3.5.2-beta0015 | 686 | 11/12/2020 | |
| 3.5.2-beta0010 | 621 | 11/12/2020 | |
| 3.5.2-beta0006 | 646 | 11/10/2020 | |
| 3.5.2-beta0003 | 1,111 | 10/14/2020 | |
| 3.5.2-beta0001 | 15,892 | 10/12/2020 | |
| 1.0.0 | 10,542 | 6/1/2018 |