trbenning / serilog-sinks-xunit

The xunit test output sink for Serilog
MIT License
83 stars 8 forks source link

Add extension method simplifying the creation of test logger #7

Closed feO2x closed 5 years ago

feO2x commented 5 years ago

First of all, thanks for this sink, I use it in my daily programming life.

I would like to add an extension method that simplifies the creation of ILogger instances that log to ITestOutputHelper. Usually, in my test projects, it looks like this:

public static ILogger CreateTestLogger(this ITestOutputHelper output) =>
    new LoggerConfiguration().WriteTo.TestOutput(output).CreateLogger();

Of course, we could add optional parameters for e.g. outputTemplate and LogEventLevel to this method.

In the end, it could be used like this:

public sealed class MyTestClass
{
    private readonly TheClassIWantToTest _testTarget;

    public MyTestClass(ITestOutputHelper output)
    {
        _testTarget = new TheClassIWantToTest(output.CreateTestLogger());
    }

    // Facts and Theories omitted for the sake of brevity
}

IMO, this would simplify the calling code tremendously. What's your opinion?

trbenning commented 5 years ago

I like it. Do you want to submit a pull request?

feO2x commented 5 years ago

Yes, I will implement this in the upcoming days if that's OK for you.

feO2x commented 5 years ago

Is it OK if I add these methods to the TestOutputLoggerConfigurationExtensions class?

trbenning commented 5 years ago

I think I'd prefer adding a new class called TestOutputHelperExtensions.

trbenning commented 5 years ago

Merged in 1.0.7