Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to scope the TestingLoggerFactory #147

Merged
merged 4 commits into from Sep 10, 2019

Conversation

@danielmarbach
Copy link
Member

commented Sep 6, 2019

The testing logging factory is kind of broken. When you write a test like

[Test]
    public async Task ShouldLogCorrectly()
    {
        var logStatements = new StringBuilder();

        LogManager.Use<TestingLoggerFactory>()
            .WriteTo(new StringWriter(logStatements));

        var handler = new MyHandlerWithLogging();

        await handler.Handle(new MyRequest(), new TestableMessageHandlerContext())
            .ConfigureAwait(false);

        StringAssert.Contains("Some log message", logStatements.ToString());
    }

and then write another test like

[Test]
    public async Task ShouldLogCorrectlyASecondTime()
    {
        var logStatements = new StringBuilder();

        LogManager.Use<TestingLoggerFactory>()
            .WriteTo(new StringWriter(logStatements));

        var handler = new MyHandlerWithLogging();

        await handler.Handle(new MyRequest(), new TestableMessageHandlerContext())
            .ConfigureAwait(false);

        StringAssert.Contains("Some log message", logStatements.ToString());
    }

Whichever test executed first the string writer is set. So in case the first step executes the second test will fail. To better explain those side effects I recently augmented the documentation a bit to show how the current implementation can be used

[SetUpFixture]
public class LoggingSetupFixture
{
    static readonly StringBuilder logStatements = new StringBuilder();

    [OneTimeSetUp]
    public void SetUp()
    {
        LogManager.Use<TestingLoggerFactory>()
            .WriteTo(new StringWriter(logStatements));
    }

    public static string LogStatements => logStatements.ToString();

    public static void Clear()
    {
        logStatements.Clear();
    }
}

[TestFixture]
public class LoggingTests
{
    #region LoggerTesting

    [SetUp]
    public void SetUp()
    {
        LoggingSetupFixture.Clear();
    }

    [Test]
    public async Task ShouldLogCorrectly()
    {
        var logStatements = new StringBuilder();

        LogManager.Use<TestingLoggerFactory>()
            .WriteTo(new StringWriter(logStatements));

        var handler = new MyHandlerWithLogging();

        await handler.Handle(new MyRequest(), new TestableMessageHandlerContext())
            .ConfigureAwait(false);

        StringAssert.Contains("Some log message", logStatements.ToString());
        StringAssert.Contains("Some log message", LoggingSetupFixture.LogStatements);
    }

    #endregion
} 

But this clearly shows that the testing logging factory cannot be used concurrently. So if you have any test parallelization or multiple handlers involved it would fail. This PR addressed this by introducing a scope that allows managing a writer and a log level for the lifetime of the scope. The tests should give an indication of the benefits of this approach

@danielmarbach danielmarbach force-pushed the loggingfactory branch from 6d24b1d to 35afc8f Sep 6, 2019

@danielmarbach danielmarbach changed the title WIP Adding ability to scope the TestingLoggerFactory Adding ability to scope the TestingLoggerFactory Sep 6, 2019

@danielmarbach danielmarbach requested a review from timbussmann Sep 6, 2019

@danielmarbach danielmarbach added this to the 7.2.0 milestone Sep 9, 2019

@danielmarbach danielmarbach changed the title Adding ability to scope the TestingLoggerFactory Ability to scope the TestingLoggerFactory Sep 9, 2019

@timbussmann timbussmann merged commit 56ab069 into master Sep 10, 2019

7 checks passed

Compile Finished TeamCity Build NServiceBus / NServiceBus.Testing / 1. Compile : Running
Details
Inspections Finished TeamCity Build NServiceBus / NServiceBus.Testing / 2. Inspections : Running
Details
Test .NET Core on Linux Finished TeamCity Build NServiceBus / NServiceBus.Testing / 3.3 Test (.NET Core on Linux) : Tests passed: 147
Details
Test .NET Core on Windows Finished TeamCity Build NServiceBus / NServiceBus.Testing / 3.2 Test (.NET Core on Windows) : Tests passed: 147
Details
Test .NET Framework on Windows Finished TeamCity Build NServiceBus / NServiceBus.Testing / 3.1 Test (.NET Framework on Windows) : Tests passed: 147
Details
WIP Ready for review
Details
license/cla Contributor License Agreement is signed.
Details

@timbussmann timbussmann deleted the loggingfactory branch Sep 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.