Git Product home page Git Product logo

r8.xunitlogger's Introduction

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

r8.xunitlogger's People

Contributors

iamr8 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.