C3D.Extensions.Logging.Xunit 0.2.2

dotnet add package C3D.Extensions.Logging.Xunit --version 0.2.2
NuGet\Install-Package C3D.Extensions.Logging.Xunit -Version 0.2.2
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="C3D.Extensions.Logging.Xunit" Version="0.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add C3D.Extensions.Logging.Xunit --version 0.2.2
#r "nuget: C3D.Extensions.Logging.Xunit, 0.2.2"
#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 C3D.Extensions.Logging.Xunit as a Cake Addin
#addin nuget:?package=C3D.Extensions.Logging.Xunit&version=0.2.2

// Install C3D.Extensions.Logging.Xunit as a Cake Tool
#tool nuget:?package=C3D.Extensions.Logging.Xunit&version=0.2.2

C3D.Extensions.Logging.Xunit

An implementation of Microsoft.Extensions.Logging Loggers wrapping xunit output types. This allows you to capture logging output when using dotnet test etc. It can be injected into services under test, or used as a provider in DI.

It supports some minor configuration which can be added via the normal configuration sources, such as appsettings.json, usersecrets etc.

Configuration

{
	"Logging": {
		"Xunit": {
			"MinLevel": "Trace",
			"UseOffset": true,
			"LogLevel": {
				"Default": "Trace"
			}
		}
	}
}

Usage

Dependency Injection

services
	.AddSingleton(output)
	.AddLogging(logging => logging
		.ClearProviders()
		.SetMinimumLevel(LogLevel.Debug)
		.AddXunit());

Here output can be either ITestOutputHelper or IMessageSink, depending on whether you are using this from a unit test, or a class fixture.

Alternatively, you can explicitly add the output, and/or setup the logging configuration inside the AddXunit extension method.

logging
	.AddXunit(output, config=>{
	});

N.B. This will add the output object as a singleton to the ILoggingBuilder services.

Direct injection

private readonly ITestOutputHelper output;

ILogger<ClassUnderTest> log = output.CreateLogger<ClassUnderTest>();

\\ Assuming that the constuctor of ClassUnderTest takes an ILogger or ILogger<ClassUnderTest>
var sut = new ClassUnderTest(log);

\\ perform tests with logging...

Here the ILogger creation line might replace something like

ILogger<ClassUnderTest> log = Microsoft.Extensions.Logging.Abstractions.NullLoggerFactory.Instance.CreateLogger<ClassUnderTest>();

Factory

You can also create a LoggerFactory if you need to using something like

private readonly ITestOutputHelper output;

var loggerFactory = LoggerFactory.Create(builder => builder.AddXunit(output));

Fixtures

The same mechanisms used in tests based on injecting ITestOutputHelper into the test class, can be used in fixtures. However, in fixtures you inject IMessageSink. The same extension methods are available for both interfaces.

Utilities

As you may want to check the logged output for content in your tests (either for something existing or not existing), there are 2 utility classes provided.

  • LoggingMessageSink which implements IMessageSink
  • LoggingTestOutputHelper which implements ITestOutputHelper

These both have a ClearMessages function (which may be required in a fixture to ensure output from one test does not affect another), and a Messages property.

Examples of how to use these can be found in the unit tests at CZEMacLeod/C3D.Extensions.Logging.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  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 is compatible.  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. 
.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 is compatible. 
.NET Framework net461 was computed.  net462 is compatible.  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)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on C3D.Extensions.Logging.Xunit:

Package Downloads
C3D.Extensions.Playwright.AspNetCore.Xunit

Assembly Version: 0.1.0.0 File Version: 0.1.32.31804 Informational Version: 0.1.32+3c7cdaa8b1 Build Configuration: Release

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.2.2 3,107 8/11/2023
0.1.17 174 8/11/2023