Logging: WIP Json logging

This commit is contained in:
Jonathan Jenne 2022-12-16 09:03:21 +01:00
parent db1d3fb197
commit a00ad5a9c0

View File

@ -81,6 +81,7 @@ Public Class LogConfig
Private Const TARGET_ERROR As String = "errorTarget" Private Const TARGET_ERROR As String = "errorTarget"
Private Const TARGET_DEBUG As String = "debugTarget" Private Const TARGET_DEBUG As String = "debugTarget"
Private Const TARGET_TRACE As String = "traceTarget" Private Const TARGET_TRACE As String = "traceTarget"
Private Const TARGET_JSON As String = "jsonTarget"
'Private Const TARGET_MEMORY As String = "memoryTarget" 'Private Const TARGET_MEMORY As String = "memoryTarget"
Private Const LOG_FORMAT_BASE As String = "${time}|${logger:shortName=True}|${level:uppercase=true}" Private Const LOG_FORMAT_BASE As String = "${time}|${logger:shortName=True}|${level:uppercase=true}"
@ -101,8 +102,9 @@ Public Class LogConfig
Private ReadOnly _basePath As String = _failSafePath Private ReadOnly _basePath As String = _failSafePath
Private _config As LoggingConfiguration Private _config As LoggingConfiguration
Private _isDebug As Boolean = False Private _EnableDebugLogging As Boolean = False
Private _isTrace As Boolean = False Private _EnableTraceLogging As Boolean = False
Private _EnableJsonLogging As Boolean = False
#End Region #End Region
#Region "Public Properties" #Region "Public Properties"
@ -137,21 +139,31 @@ Public Class LogConfig
''' <returns>True, if debug log will be written. False otherwise.</returns> ''' <returns>True, if debug log will be written. False otherwise.</returns>
Public Property Debug As Boolean Public Property Debug As Boolean
Get Get
Return _isDebug Return _EnableDebugLogging
End Get End Get
Set(isDebug As Boolean) Set(value As Boolean)
_isDebug = isDebug _EnableDebugLogging = value
ReloadConfig(isDebug, _isTrace) ReloadConfig()
End Set End Set
End Property End Property
Public Property Trace As Boolean Public Property Trace As Boolean
Get Get
Return _isTrace Return _EnableTraceLogging
End Get End Get
Set(isTrace As Boolean) Set(value As Boolean)
_isTrace = isTrace _EnableTraceLogging = value
ReloadConfig(_isDebug, isTrace) ReloadConfig()
End Set
End Property
Public Property EnableJsonLog As Boolean
Get
Return _EnableJsonLogging
End Get
Set(value As Boolean)
_EnableJsonLogging = value
ReloadConfig()
End Set End Set
End Property End Property
@ -478,9 +490,7 @@ Public Class LogConfig
''' <summary> ''' <summary>
''' Reconfigures and re-adds all loggers, optionally adding the debug rule. ''' Reconfigures and re-adds all loggers, optionally adding the debug rule.
''' </summary> ''' </summary>
''' <param name="Debug">Adds the Debug rule if true.</param> Private Sub ReloadConfig()
''' <param name="Trace">Adds the Trace rule if true.</param>
Private Sub ReloadConfig(Optional Debug As Boolean = False, Optional Trace As Boolean = False)
Dim oLogger = GetLogger() Dim oLogger = GetLogger()
' Clear Logging Rules ' Clear Logging Rules
@ -489,15 +499,22 @@ Public Class LogConfig
' Add default rules ' Add default rules
AddDefaultRules(_config) AddDefaultRules(_config)
' Add json rule, if configured
If _EnableJsonLogging = True Then
oLogger.Info("JSON Logging is now Enabled.")
_config.AddRule(LogLevel.Debug, LogLevel.Error, TARGET_JSON)
End If
' Add debug rule, if configured ' Add debug rule, if configured
If Debug = True Then If _EnableDebugLogging = True Then
_config.AddRule(LogLevel.Debug, LogLevel.Error, TARGET_DEBUG) _config.AddRule(LogLevel.Debug, LogLevel.Error, TARGET_DEBUG)
oLogger.Info("DEBUG Logging is now Enabled.") oLogger.Info("DEBUG Logging is now Enabled.")
Else Else
oLogger.Debug("DEBUG Logging is now Disabled.") oLogger.Debug("DEBUG Logging is now Disabled.")
End If End If
If Trace = True Then ' Add trace rule, if configured
If _EnableTraceLogging = True Then
_config.AddRule(LogLevel.Trace, LogLevel.Error, TARGET_TRACE) _config.AddRule(LogLevel.Trace, LogLevel.Error, TARGET_TRACE)
End If End If
@ -506,11 +523,34 @@ Public Class LogConfig
End Sub End Sub
#Region "Log Targets" #Region "Log Targets"
Private Function GetJsonLogTarget(basePath As String) As FileTarget
Dim defaultLog As New FileTarget() With {
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT),
.Name = TARGET_JSON,
.Layout = LOG_FORMAT_DEFAULT,
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
.ArchiveEvery = ARCHIVE_EVERY,
.KeepFileOpen = KEEP_FILES_OPEN,
.Encoding = Text.Encoding.Unicode
}
Return defaultLog
End Function
Private Function GetDefaultLogTarget(basePath As String) As FileTarget Private Function GetDefaultLogTarget(basePath As String) As FileTarget
Dim oJsonLayout = New Layouts.JsonLayout
oJsonLayout.Attributes.Add(New Layouts.JsonAttribute("level", "${level}"))
oJsonLayout.Attributes.Add(New Layouts.JsonAttribute("message", "${message}"))
oJsonLayout.Attributes.Add(New Layouts.JsonAttribute("date", "${shortdate}"))
oJsonLayout.Attributes.Add(New Layouts.JsonAttribute("product", "${var:product}"))
oJsonLayout.Attributes.Add(New Layouts.JsonAttribute("suffix", "${var:suffix}"))
oJsonLayout.Attributes.Add(New Layouts.JsonAttribute("module", "${event-properties:item=ModuleName}"))
oJsonLayout.Attributes.Add(New Layouts.JsonAttribute("exception", "${exception:format=Message,StackTrace:innerFormat=Message:maxInnerExceptionLevel=3}"))
Dim defaultLog As New FileTarget() With { Dim defaultLog As New FileTarget() With {
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT), .FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT),
.Name = TARGET_DEFAULT, .Name = TARGET_DEFAULT,
.Layout = LOG_FORMAT_DEFAULT, .Layout = oJsonLayout,
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT, .MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
.ArchiveEvery = ARCHIVE_EVERY, .ArchiveEvery = ARCHIVE_EVERY,
.KeepFileOpen = KEEP_FILES_OPEN, .KeepFileOpen = KEEP_FILES_OPEN,
@ -565,9 +605,9 @@ Public Class LogConfig
End Function End Function
Private Function GetTraceLogTarget(basePath As String) As FileTarget Private Function GetTraceLogTarget(basePath As String) As FileTarget
Dim debugLog As New FileTarget() With { Dim traceLog As New FileTarget() With {
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_TRACE), .FileName = Path.Combine(basePath, FILE_NAME_FORMAT_TRACE),
.Name = TARGET_DEBUG, .Name = TARGET_TRACE,
.Layout = LOG_FORMAT_DEBUG, .Layout = LOG_FORMAT_DEBUG,
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEBUG_DETAIL, .MaxArchiveFiles = MAX_ARCHIVE_FILES_DEBUG_DETAIL,
.ArchiveEvery = ARCHIVE_EVERY, .ArchiveEvery = ARCHIVE_EVERY,
@ -578,18 +618,7 @@ Public Class LogConfig
.Encoding = Text.Encoding.Unicode .Encoding = Text.Encoding.Unicode
} }
Return debugLog Return traceLog
End Function End Function
'Private Function GetMemoryDebugTarget() As MemoryTarget
' Dim memoryLog As New MemoryTarget() With {
' .Layout = LOG_FORMAT_DEBUG,
' .Name = TARGET_MEMORY,
' .OptimizeBufferReuse = True,
' .MaxLogsCount = MAX_MEMORY_LOG_COUNT
' }
' Return memoryLog
'End Function
#End Region #End Region
End Class End Class