Skip to content

iamr8/R8.XunitLogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

R8.XunitLogger

Seamlessly integrate Xunit's ITestOutputHelper with Microsoft.Extensions.Logging using netstandard2.1, net6.0, net7.0 and net8.0. Capture logs from any layer of your application and send them to your test output, making debugging and integration testing a breeze. With minimal configuration, you can monitor log messages, helping to ensure your application runs smoothly.

Nuget Nuget Commit

Installation

dotnet add package R8.XunitLogger

Options

Option Description Default Value
MinimumLevel The minimum level of log messages to be written to the test output LogLevel.Information
IncludeTimestamp Whether to include timestamp in log messages true
IncludeScopes Whether to include scopes in log messages false
ColorBehavior Whether to enable colored log levels LoggerColorBehavior.Default
Categories The categories (namespaces) of log messages to be written to the test output null
ServiceProvider The service provider to be get appsettings.json from IConfiguration null

Colored Log Levels

Microsoft Visual Studio does not support colored log levels. However, JetBrains Rider does support colored log levels. If you want to enable colored log levels, you can use LoggerColorBehavior enum. If the value is set to LoggerColorBehavior.Default, the log levels will be colored in JetBrains Rider and will not be colored in Microsoft Visual Studio.


Usage

For simple testing:

public class UnitTest
{
    public UnitTest(ITestOutputHelper outputHelper)
    {
        _loggerFactory = new LoggerFactory().AddXunit(outputHelper, options =>
        {
            // ... options
        });
    }
}

For testing with Microsoft.Extensions.DependencyInjection:

public class IntegrationTest : IXunitLogProvider
{
    public IntegrationTest(ITestOutputHelper outputHelper)
    {
        var serviceProvider = new ServiceCollection()
            .AddLogging()
            .AddXunitLogger(message => OnWriteLine?.Invoke(message), options => 
            {
                // ... options
            })
            .BuildServiceProvider();
        this.OnWriteLine += outputHelper.WriteLine;
    }

    public event Action<string> OnWriteLine;
}

See more examples here

About

Simple logging integration between Xunit's ITestOutputHelper and Microsoft.Extensions.Logging using netstandard2.1

Topics

Resources

License

Stars

Watchers

Forks

Languages