breaking change in ContextLogger constructor

This commit is contained in:
Timerix22 2023-02-21 21:45:02 +06:00
parent f141f4a599
commit 021e9d1b65
3 changed files with 10 additions and 13 deletions

View File

@ -17,7 +17,7 @@ public static class DTLibInternalLogging
public static void SetLogger(ILogger logger) public static void SetLogger(ILogger logger)
{ {
_loggerContext = new ContextLogger(logger, "DTLib"); _loggerContext = new ContextLogger("DTLib",logger);
PublicLog.LogEvent+=LogHandler; PublicLog.LogEvent+=LogHandler;
} }

View File

@ -6,11 +6,17 @@ public class ContextLogger : ILogger
public ILogger ParentLogger; public ILogger ParentLogger;
public readonly string Context; public readonly string Context;
public ContextLogger(ILogger parentLogger, string context) public ContextLogger(string context,ILogger parentLogger)
{ {
ParentLogger = parentLogger; ParentLogger = parentLogger;
Context = context; Context = context;
} }
/// Appends subContext to Context
public void Log(string subContext, LogSeverity severity, object message, ILogFormat format)
{
ParentLogger.Log($"{Context}/{subContext}", severity, message, format);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Log(LogSeverity severity, object message) public void Log(LogSeverity severity, object message)
@ -42,10 +48,7 @@ public class ContextLogger : ILogger
public void LogError(Exception ex) public void LogError(Exception ex)
=> ParentLogger.LogError(Context, ex); => ParentLogger.LogError(Context, ex);
public void Dispose() public void Dispose() => ParentLogger.Dispose();
{
ParentLogger.Dispose();
}
public ILogFormat Format => ParentLogger.Format; public ILogFormat Format => ParentLogger.Format;
@ -72,10 +75,4 @@ public class ContextLogger : ILogger
get => ParentLogger.ErrorLogEnabled; get => ParentLogger.ErrorLogEnabled;
set => ParentLogger.ErrorLogEnabled = value; set => ParentLogger.ErrorLogEnabled = value;
} }
/// Appends subContext to Context
public void Log(string subContext, LogSeverity severity, object message, ILogFormat format)
{
ParentLogger.Log($"{Context}/{subContext}", severity, message, format);
}
} }

View File

@ -25,7 +25,7 @@ public static class Program
System.Console.InputEncoding = Encoding.UTF8; System.Console.InputEncoding = Encoding.UTF8;
Logger=new CompositeLogger(new ConsoleLogger(), Logger=new CompositeLogger(new ConsoleLogger(),
new FileLogger("logs", "DTLib.Tests")); new FileLogger("logs", "DTLib.Tests"));
var mainContext = new ContextLogger(Logger, "Main"); var mainContext = new ContextLogger("Main", Logger);
DTLibInternalLogging.SetLogger(Logger); DTLibInternalLogging.SetLogger(Logger);
try try