diff --git a/Common/Logging/ILogger.cs b/Common/Logging/ILogger.cs index a229e54..7469089 100644 --- a/Common/Logging/ILogger.cs +++ b/Common/Logging/ILogger.cs @@ -8,10 +8,17 @@ public interface ILogger { /// - /// Event that is thrown whenever a new log entry was entered with any Log method. + /// Event that is thrown for all new log entries that have higher or equal log level to the . /// event EventHandler? NewLogEntry; + /// + /// Currently configured minimum log level. + /// Log entries that are written with lower than the configured are not logged. + /// Also the event is not thrown. + /// + LogLevel CurrentMinimumLogLevel { get; } + /// /// Initialize the Logger once before using it. /// Sets the minimum to . Therefore all log entries will be logged. diff --git a/Common/Logging/SerilogLogger.cs b/Common/Logging/SerilogLogger.cs index cb99569..7acb1ed 100644 --- a/Common/Logging/SerilogLogger.cs +++ b/Common/Logging/SerilogLogger.cs @@ -12,8 +12,12 @@ namespace Common.Logger; public class SerilogLogger : Logging.ILogger { private readonly LoggingLevelSwitch loggingLevelSwitch; + + public event EventHandler? NewLogEntry; + public LogLevel CurrentMinimumLogLevel { get; private set; } = LogLevel.Undefined; + /// /// Constructor of a Logger that uses the Serilog package to create log entries and distributes them into different sinks. /// @@ -67,6 +71,7 @@ public class SerilogLogger : Logging.ILogger public void SetMinimumLogLevel(LogLevel newMinimumLogLevel) { this.loggingLevelSwitch.MinimumLevel = this.ConvertGenericToSerilogLogLevel(newMinimumLogLevel); + this.CurrentMinimumLogLevel = newMinimumLogLevel; } private LogEventLevel ConvertGenericToSerilogLogLevel(LogLevel newMinimumLogLevel) => newMinimumLogLevel switch @@ -88,6 +93,7 @@ public class SerilogLogger : Logging.ILogger private void CreateLogEntry(LogEntry logEntry) { var serilogEventLevel = this.ConvertGenericToSerilogLogLevel(logEntry.LogLevel); + // formatting is done inside logEntry.ToString() method => therefore log plain message text with right category switch (serilogEventLevel) { @@ -118,6 +124,12 @@ public class SerilogLogger : Logging.ILogger default: throw new NotImplementedException($"'{nameof(CreateLogEntry)}()' does not contain an implementation for {nameof(LogEventLevel)} {serilogEventLevel}."); } + + // Raise event (only if the log is wanted due to the configured log level) + if (logEntry.LogLevel <= this.CurrentMinimumLogLevel) + { + this.NewLogEntry?.Invoke(this, new NewLogEntryEventArgs(logEntry)); + } }