Skip to content

Commit

Permalink
Loggers now get the active writer instead of the writer they were cre…
Browse files Browse the repository at this point in the history
…ated with.
  • Loading branch information
manfred-brands committed Dec 31, 2024
1 parent 2667029 commit 25236bd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static void Initialize(string logName, InternalTraceLevel level)
/// </summary>
public static Logger GetLogger(string name, InternalTraceLevel level)
{
return new Logger(name, level, _traceWriter);
return new Logger(name, level, () => _traceWriter);
}

/// <summary>
Expand All @@ -80,7 +80,7 @@ public static Logger GetLogger(Type type, InternalTraceLevel level)
/// </summary>
public static Logger GetLogger(string name)
{
return new Logger(name, DefaultTraceLevel, _traceWriter);
return new Logger(name, DefaultTraceLevel, () => _traceWriter);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -30,6 +32,8 @@ internal InternalTraceWriter(string logPath)
/// <param name="writer"></param>
public InternalTraceWriter(TextWriter writer)
{
Guard.ArgumentNotNull(writer, nameof(writer));

_writer = writer;
}

Expand Down Expand Up @@ -88,6 +92,8 @@ protected override void Dispose(bool disposing)
{
if (disposing && _writer != null)
{
Debugger.Break();

_writer.Flush();
_writer.Dispose();
_writer = null!;
Expand All @@ -102,8 +108,7 @@ protected override void Dispose(bool disposing)
/// </summary>
public override void Flush()
{
if ( _writer != null )
_writer.Flush();
_writer.Flush();
}
}
}
12 changes: 6 additions & 6 deletions src/NUnitEngine/nunit.engine.core/Internal/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextWriter> _getCurrentWriter;

public InternalTraceLevel TraceLevel { get; }

Expand All @@ -23,11 +23,11 @@ public class Logger : ILogger
/// </summary>
/// <param name="fullName">The name.</param>
/// <param name="level">The log level.</param>
/// <param name="writer">The writer where logs are sent.</param>
public Logger(string fullName, InternalTraceLevel level, TextWriter writer)
/// <param name="getCurrentWriter">The writer where logs are sent.</param>
public Logger(string fullName, InternalTraceLevel level, Func<TextWriter> getCurrentWriter)
{
TraceLevel = level;
_writer = writer;
_getCurrentWriter = getCurrentWriter;

var index = fullName.LastIndexOf('.');
_name = index >= 0 ? fullName.Substring(index + 1) : fullName;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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
Expand Down

0 comments on commit 25236bd

Please sign in to comment.