Skip to content

Commit

Permalink
Ability to scope the TestingLoggerFactory (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmarbach authored and timbussmann committed Sep 10, 2019
1 parent 43fcfa9 commit 56ab069
Show file tree
Hide file tree
Showing 8 changed files with 345 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ namespace NServiceBus.Testing
public class TestingLoggerFactory : NServiceBus.Logging.LoggingFactoryDefinition
{
public TestingLoggerFactory() { }
public System.IDisposable BeginScope(System.IO.TextWriter writer, NServiceBus.Logging.LogLevel level = 0) { }
protected override NServiceBus.Logging.ILoggerFactory GetLoggingFactory() { }
public void Level(NServiceBus.Logging.LogLevel level) { }
public void WriteTo(System.IO.TextWriter writer) { }
Expand Down
50 changes: 50 additions & 0 deletions src/NServiceBus.Testing.Tests/Logging/LoggingForFixtureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace NServiceBus.Testing.Tests.Logging
{
using System;
using System.IO;
using NServiceBus.Logging;
using NUnit.Framework;

[TestFixture]
[Parallelizable]
public class LoggingForFixtureTests
{
StringWriter writer;
IDisposable scope;

[SetUp]
public void Setup()
{
writer = new StringWriter();

this.scope = LogManager.Use<TestingLoggerFactory>()
.BeginScope(writer);
}

[Test]
public void Should_write_first_independent_from_other()
{
var Logger = LogManager.GetLogger<LoggingForFixtureTests>();
Logger.Debug("First");

StringAssert.Contains("NServiceBus.Testing.Tests.Logging.LoggingForFixtureTests First", writer.ToString());
StringAssert.DoesNotContain("NServiceBus.Testing.Tests.Logging.LoggingForFixtureTests Second", writer.ToString());
}

[Test]
public void Should_write_second_independent_from_other()
{
var Logger = LogManager.GetLogger<LoggingForFixtureTests>();
Logger.Debug("Second");

StringAssert.Contains("NServiceBus.Testing.Tests.Logging.LoggingForFixtureTests Second", writer.ToString());
StringAssert.DoesNotContain("NServiceBus.Testing.Tests.Logging.LoggingForFixtureTests First", writer.ToString());
}

[TearDown]
public void Teardown()
{
scope.Dispose();
}
}
}
144 changes: 144 additions & 0 deletions src/NServiceBus.Testing.Tests/Logging/LoggingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
namespace NServiceBus.Testing.Tests.Logging
{
using System;
using System.IO;
using NServiceBus.Logging;
using NUnit.Framework;

[TestFixture]
public class LoggingTests
{
[TearDown]
public void Teardown()
{
SomeClassThatUsesStaticLogger.Reset();
}

[Test]
public void Scoped_Writer_should_be_honored()
{
var firstStringWriter = new StringWriter();
var loggerFactory = LogManager.Use<TestingLoggerFactory>();
using (loggerFactory.BeginScope(firstStringWriter))
{
var firstInstance = new SomeClassThatUsesStaticLogger();
firstInstance.DoSomething();
}

var secondStringWriter = new StringWriter();
using (loggerFactory.BeginScope(secondStringWriter))
{
var secondInstance = new SomeClassThatUsesStaticLogger();
secondInstance.DoSomething();
}

var firstLogString = firstStringWriter.ToString();
var secondLogString = secondStringWriter.ToString();

Assert.AreNotEqual(firstLogString, secondLogString);
StringAssert.Contains("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 0", firstLogString);
StringAssert.DoesNotContain("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 1", firstLogString);
StringAssert.Contains("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 1", secondLogString);
StringAssert.DoesNotContain("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 0", secondLogString);
}

[Test]
public void Scoped_Loglevel_should_be_honored()
{
var firstStringWriter = new StringWriter();
var loggerFactory = LogManager.Use<TestingLoggerFactory>();
using (loggerFactory.BeginScope(firstStringWriter, LogLevel.Warn))
{
var firstInstance = new SomeClassThatUsesStaticLogger();
firstInstance.DoSomething();
}

var secondStringWriter = new StringWriter();
using (loggerFactory.BeginScope(secondStringWriter))
{
var secondInstance = new SomeClassThatUsesStaticLogger();
secondInstance.DoSomething();
}

var firstLogString = firstStringWriter.ToString();
var secondLogString = secondStringWriter.ToString();

Assert.AreNotEqual(firstLogString, secondLogString);
Assert.IsEmpty(firstLogString);
StringAssert.Contains("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 1", secondLogString);
}

[Test]
public void Global_Writer_should_be_honored()
{
var loggerFactory = LogManager.Use<TestingLoggerFactory>();
var globalWriter = new StringWriter();
loggerFactory.WriteTo(globalWriter);

var firstInstance = new SomeClassThatUsesStaticLogger();
firstInstance.DoSomething();

var secondStringWriter = new StringWriter();
using (loggerFactory.BeginScope(secondStringWriter))
{
var secondInstance = new SomeClassThatUsesStaticLogger();
secondInstance.DoSomething();
}

var globalLogString = globalWriter.ToString();
var scopedLogString = secondStringWriter.ToString();

Assert.AreNotEqual(globalLogString, scopedLogString);
StringAssert.Contains("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 0", globalLogString);
StringAssert.DoesNotContain("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 1", globalLogString);
StringAssert.Contains("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 1", scopedLogString);
StringAssert.DoesNotContain("NServiceBus.Testing.Tests.Logging.LoggingTests+SomeClassThatUsesStaticLogger 0", scopedLogString);
}

[Test]
public void Scope_cannot_be_nested()
{
Assert.Throws<InvalidOperationException>(() =>
{
var loggerFactory = LogManager.Use<TestingLoggerFactory>();
using (loggerFactory.BeginScope(new StringWriter()))
using (loggerFactory.BeginScope(new StringWriter()))
{
}
});
}

[Test]
public void NoScope_does_work()
{
LogManager.Use<TestingLoggerFactory>();

var secondInstance = new SomeClassThatUsesStaticLogger();
secondInstance.DoSomething();
}

class SomeClassThatUsesStaticLogger
{
public SomeClassThatUsesStaticLogger()
{
InstanceCounter = instanceCounter++;
}

public int InstanceCounter { get; }

public void DoSomething()
{
Logger.Debug(InstanceCounter.ToString());
}

public static void Reset()
{
instanceCounter = 0;
}

static int instanceCounter;

static ILog Logger = LogManager.GetLogger<SomeClassThatUsesStaticLogger>();
}
}
}
Loading

0 comments on commit 56ab069

Please sign in to comment.