Skip to main content
Version: v2.0.0

Test framework logging

This page describes functionality related to logging in tests.

The Arcus.Testing.Logging.Xunit library provides a XunitTestLogger type that's an implementation of the abstracted Microsoft Ilogger inside the xUnit test framework.

Installation

The following functionality is available when installing this package:

PM> Install-Package -Name Arcus.Testing.Logging.Xunit # for xUnit v2 tests
PM> Install-Package -Name Arcus.Testing.Logging.Xunit.v3 # for xUnit v3 tests

Example

Log messages written to the ILogger instance will be written to the xUnit test output.

using Arcus.Testing;
using Microsoft.Extensions.Logging;
using Xunit;

public class TestClass
{
private readonly ILogger _testLogger;

public TestClass(ITestOutputHelper outputWriter)
{
_testLogger = new XunitTestLogger(outputWriter);
}
}

In the same fashion there is an extension to add a ILoggerProvider to a Microsoft Logging setup:

using Arcus.Testing;
using Microsoft.Extensions.Logging;

ILoggerFactory factory = LoggerFactory.Create(logging =>
{
logging.SetMinimumLevel(LogLevel.Warning)
.AddFilter("Example.DefaultService", LogLevel.Trace)
.AddFile("logs/test-{0:yyyy}-{0:MM}-{0:dd}.log")
.AddXunitTestLogging(outputWriter);
});
ILogger logger = factory.CreateLogger<TestClass>();