diff --git a/src/NUnitEngine/nunit.engine.core.tests/Internal/Logging/LoggingTests.cs b/src/NUnitEngine/nunit.engine.core.tests/Internal/Logging/LoggingTests.cs index 717486113..fa061b53c 100644 --- a/src/NUnitEngine/nunit.engine.core.tests/Internal/Logging/LoggingTests.cs +++ b/src/NUnitEngine/nunit.engine.core.tests/Internal/Logging/LoggingTests.cs @@ -16,7 +16,7 @@ public void LoggerSelectsMessagesToWrite( [ValueSource(nameof(LEVELS))] InternalTraceLevel msgLevel) { var writer = new StringWriter(); - var logger = new Logger("MyLogger", logLevel, writer); + var logger = new Logger("MyLogger", logLevel, () => writer); Assert.That(logger.TraceLevel, Is.EqualTo(logLevel)); diff --git a/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTrace.cs b/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTrace.cs index 582de1dde..3f69cd3c4 100644 --- a/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTrace.cs +++ b/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTrace.cs @@ -64,7 +64,7 @@ public static void Initialize(string logName, InternalTraceLevel level) /// public static Logger GetLogger(string name, InternalTraceLevel level) { - return new Logger(name, level, _traceWriter); + return new Logger(name, level, () => _traceWriter); } /// @@ -80,7 +80,7 @@ public static Logger GetLogger(Type type, InternalTraceLevel level) /// public static Logger GetLogger(string name) { - return new Logger(name, DefaultTraceLevel, _traceWriter); + return new Logger(name, DefaultTraceLevel, () => _traceWriter); } /// diff --git a/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTraceWriter.cs b/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTraceWriter.cs index 0bf112522..4046ac30a 100644 --- a/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTraceWriter.cs +++ b/src/NUnitEngine/nunit.engine.core/Internal/Logging/InternalTraceWriter.cs @@ -1,5 +1,7 @@ // Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt +using System.Diagnostics; using System.IO; +using NUnit.Common; namespace NUnit.Engine.Internal { @@ -30,6 +32,8 @@ internal InternalTraceWriter(string logPath) /// public InternalTraceWriter(TextWriter writer) { + Guard.ArgumentNotNull(writer, nameof(writer)); + _writer = writer; } @@ -88,6 +92,8 @@ protected override void Dispose(bool disposing) { if (disposing && _writer != null) { + Debugger.Break(); + _writer.Flush(); _writer.Dispose(); _writer = null!; @@ -102,8 +108,7 @@ protected override void Dispose(bool disposing) /// public override void Flush() { - if ( _writer != null ) - _writer.Flush(); + _writer.Flush(); } } } \ No newline at end of file diff --git a/src/NUnitEngine/nunit.engine.core/Internal/Logging/Logger.cs b/src/NUnitEngine/nunit.engine.core/Internal/Logging/Logger.cs index 26eb1578c..1b8fa9399 100644 --- a/src/NUnitEngine/nunit.engine.core/Internal/Logging/Logger.cs +++ b/src/NUnitEngine/nunit.engine.core/Internal/Logging/Logger.cs @@ -14,7 +14,7 @@ public class Logger : ILogger private const string TraceFmt = "{0} {1,-5} [{2,2}] {3}: {4}"; private readonly string _name; - private readonly TextWriter _writer; + private readonly Func _getCurrentWriter; public InternalTraceLevel TraceLevel { get; } @@ -23,11 +23,11 @@ public class Logger : ILogger /// /// The name. /// The log level. - /// The writer where logs are sent. - public Logger(string fullName, InternalTraceLevel level, TextWriter writer) + /// The writer where logs are sent. + public Logger(string fullName, InternalTraceLevel level, Func getCurrentWriter) { TraceLevel = level; - _writer = writer; + _getCurrentWriter = getCurrentWriter; var index = fullName.LastIndexOf('.'); _name = index >= 0 ? fullName.Substring(index + 1) : fullName; @@ -111,7 +111,7 @@ public void Debug(string format, params object[] args) private void Log(InternalTraceLevel level, string message) { - if (_writer != null && TraceLevel >= level) + if (_getCurrentWriter != null && TraceLevel >= level) WriteLog(level, message); } @@ -123,7 +123,7 @@ private void Log(InternalTraceLevel level, string format, params object[] args) private void WriteLog(InternalTraceLevel level, string message) { - _writer.WriteLine(TraceFmt, + _getCurrentWriter.Invoke().WriteLine(TraceFmt, DateTime.Now.ToString(TimeFmt), level, #if NET20