Skip to content

Commit

Permalink
Ensure Loggers use late bound TraceLevel and Writer.
Browse files Browse the repository at this point in the history
  • Loading branch information
manfred-brands committed Jan 2, 2025
1 parent b1f08dc commit cce6f59
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt

using System.IO;
using NUnit.Engine.Internal;
using NUnit.Framework;

namespace NUnit.Engine.Internal.Logging
Expand All @@ -16,7 +15,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 @@ -63,7 +63,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 @@ -79,15 +79,15 @@ 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>
/// Get a logger named for a particular Type using the default TraceLevel.
/// </summary>
public static Logger GetLogger(Type type)
{
return GetLogger(type.FullName, DefaultTraceLevel);
return GetLogger(type.FullName);
}
}
}
19 changes: 10 additions & 9 deletions src/NUnitEngine/nunit.engine.core/Internal/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@ 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> _getWriterFn;
private readonly Func<InternalTraceLevel> _getLevelFn;

public InternalTraceLevel TraceLevel { get; }
public InternalTraceLevel TraceLevel => _getLevelFn.Invoke();

/// <summary>
/// Initializes a new instance of the <see cref="Logger"/> class.
/// </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="getLevelFn">The log level.</param>
/// <param name="getWriterFn">The writer where logs are sent.</param>
public Logger(string fullName, Func<InternalTraceLevel> getLevelFn, Func<TextWriter> getWriterFn)
{
TraceLevel = level;
_writer = writer;
_getLevelFn = getLevelFn;
_getWriterFn = getWriterFn;

var index = fullName.LastIndexOf('.');
_name = index >= 0 ? fullName.Substring(index + 1) : fullName;
Expand Down Expand Up @@ -111,7 +112,7 @@ public void Debug(string format, params object[] args)

private void Log(InternalTraceLevel level, string message)
{
if (_writer != null && TraceLevel >= level)
if (TraceLevel >= level)
WriteLog(level, message);
}

Expand All @@ -123,7 +124,7 @@ private void Log(InternalTraceLevel level, string format, params object[] args)

private void WriteLog(InternalTraceLevel level, string message)
{
_writer.WriteLine(TraceFmt,
_getWriterFn.Invoke().WriteLine(TraceFmt,
DateTime.Now.ToString(TimeFmt),
level,
#if NET20
Expand Down

0 comments on commit cce6f59

Please sign in to comment.