From 85a33b35e8a9aec036c1bb278b70bf89a0a67d99 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Thu, 17 Jun 2021 13:05:29 +0200 Subject: [PATCH] Logging: Remove Warning from Error Log, Add Sublogging with Modules, Revert own Logger methods --- Modules.Logging/LogConfig.vb | 16 ++++++--- Modules.Logging/Logger.vb | 63 ------------------------------------ 2 files changed, 12 insertions(+), 67 deletions(-) diff --git a/Modules.Logging/LogConfig.vb b/Modules.Logging/LogConfig.vb index 99a6959c..8e159d90 100644 --- a/Modules.Logging/LogConfig.vb +++ b/Modules.Logging/LogConfig.vb @@ -324,10 +324,15 @@ Public Class LogConfig End Function ''' - ''' Returns the Logger for the specified module + ''' Returns the Logger for the specified module using event-properties ''' + ''' + ''' https://github.com/NLog/NLog/wiki/EventProperties-Layout-Renderer + ''' https://stackoverflow.com/questions/31337030/separate-log-file-for-specific-class-instance-using-nlog/32065824#32065824 + ''' ''' An object of Logging.Logger - Public Function GetLoggerWithModule(ModuleName As String) As Logger + + Public Function GetLoggerFor(ModuleName As String) As Logger Dim oClassName As String = GetClassFullName() Return GetLogger(oClassName, ModuleName) End Function @@ -340,7 +345,11 @@ Public Class LogConfig Public Function GetLogger(ClassName As String, ModuleName As String) As Logger Dim oLogger = LogFactory.GetLogger(Of Logger)(ClassName) - oLogger.ModuleName = ModuleName + + If ModuleName IsNot Nothing Then + Return oLogger.WithProperty("ModuleName", $"-{ModuleName}") + End If + Return oLogger End Function @@ -431,7 +440,6 @@ Public Class LogConfig Private Sub AddDefaultRules(ByRef config As LoggingConfiguration) config.AddRuleForOneLevel(LogLevel.Error, TARGET_ERROR_EX) config.AddRuleForOneLevel(LogLevel.Fatal, TARGET_ERROR_EX) - config.AddRuleForOneLevel(LogLevel.Warn, TARGET_ERROR) config.AddRuleForOneLevel(LogLevel.Warn, TARGET_DEFAULT) config.AddRuleForOneLevel(LogLevel.Info, TARGET_DEFAULT) End Sub diff --git a/Modules.Logging/Logger.vb b/Modules.Logging/Logger.vb index 6f4253df..4337bb2f 100644 --- a/Modules.Logging/Logger.vb +++ b/Modules.Logging/Logger.vb @@ -2,70 +2,7 @@ Public Class Logger Inherits NLog.Logger - Implements ILogger - ''' - ''' Optional ModuleName to create different LogFiles for different Modules (arbitrary entities, User, File, etc.) - ''' - ''' The current ModuleName of the logger, if any. - Public Property ModuleName As String = Nothing - - - Public Overloads Sub Info(Message As String) - LogWithModuleName(Message, LogLevel.Info) - End Sub - - - Public Overloads Sub Warn(Message As String) - LogWithModuleName(Message, LogLevel.Warn) - End Sub - - - Public Overloads Sub [Error](Exception As Exception) - LogWithModuleName(Exception) - End Sub - - - Public Overloads Sub Debug(Message As String) - LogWithModuleName(Message, LogLevel.Debug) - End Sub - - - Public Overloads Sub Trace(Message As String) - LogWithModuleName(Message, LogLevel.Trace) - End Sub - - Private Sub LogWithModuleName(Exception As Exception) - Dim oEventInfo As New LogEventInfo() With { - .Exception = Exception, - .Level = LogLevel.Error, - .LoggerName = Name, - .Message = Exception.Message - } - oEventInfo = MaybeSetModuleName(oEventInfo) - - Log(oEventInfo) - End Sub - - Private Sub LogWithModuleName(Message As String, LogLevel As LogLevel) - Dim oEventInfo As New LogEventInfo(LogLevel, Name, Message) - oEventInfo = MaybeSetModuleName(oEventInfo) - - Log(oEventInfo) - End Sub - - Private Function MaybeSetModuleName(LogEventInfo As LogEventInfo) As LogEventInfo - If ModuleName IsNot Nothing AndAlso ModuleName <> String.Empty Then - LogEventInfo.Properties.Item("ModuleName") = ModuleName - End If - - Return LogEventInfo - End Function - - ''' - ''' Prints a preformatted Block including a block identifier - ''' - ''' A unique Identifier for this block, eg. DocId, FullPath, .. Public Sub NewBlock(blockId As String) Dim message As String = $"-----> Start of Block {blockId}"