Skip to content

Send application logs to the output window of your test runner

License

Notifications You must be signed in to change notification settings

ilya-chumakov/Bodrocode.Xunit.Logs

Repository files navigation

NuGet

Bodrocode.Xunit.Logs

Integration between .NET logging and xUnit output. In short, it adds logs to the output window of your test runner.

Creating a standalone logger:

public ManualCreation_Example(ITestOutputHelper output)
{
    _fooService = new FooService(output.For<FooService>());
    
    //or (the same)
    _fooService = new FooService(new XunitLogger<FooService>(output));
}

Integration with Microsoft.Extensions.DependencyInjection:

public DependencyInjection_Example(ITestOutputHelper output)
{
    var services = new ServiceCollection();
    services.AddLogging(cfg =>
    {
        cfg.AddXunit(output);
    });
    services.AddTransient<FooService>();
    var provider = services.BuildServiceProvider();

    _fooService = provider.GetRequiredService<FooService>();
}

Customization:

var logger = new XunitLogger(output, "fooCategory", cfg =>
{
    cfg.MinimumLogLevel = LogLevel.Warning;
    cfg.CategoryName = CategoryNameStyle.Short;
});

Testability:

var mock = new Mock<ITestOutputHelper>();

var logger = mock.Object.For<FooService>();

logger.Log(LogLevel.Warning, "fooMessage");

mock.Verify(m => m.WriteLine(It.IsAny<string>()), Times.Once());