-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ability to scope the TestingLoggerFactory (#147)
- Loading branch information
1 parent
43fcfa9
commit 56ab069
Showing
8 changed files
with
345 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/NServiceBus.Testing.Tests/Logging/LoggingForFixtureTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} | ||
} |
Oops, something went wrong.