This commit is contained in:
2023-01-03 16:03:51 +01:00
26 changed files with 391 additions and 138 deletions

View File

@@ -75,12 +75,14 @@ Public Class LogConfig
Private Const FILE_NAME_FORMAT_DEBUG As String = "${shortdate}-${var:product}${var:suffix}${event-properties:item=ModuleName}-Debug.log"
Private Const FILE_NAME_FORMAT_TRACE As String = "${shortdate}-${var:product}${var:suffix}${event-properties:item=ModuleName}-Trace.log"
Private Const FILE_NAME_FORMAT_ERROR As String = "${shortdate}-${var:product}${var:suffix}${event-properties:item=ModuleName}-Error.log"
Private Const FILE_NAME_FORMAT_JSON As String = "${shortdate}-${var:product}${var:suffix}${event-properties:item=ModuleName}.log.json"
Private Const TARGET_DEFAULT As String = "defaultTarget"
Private Const TARGET_ERROR_EX As String = "errorExceptionTarget"
Private Const TARGET_ERROR As String = "errorTarget"
Private Const TARGET_DEBUG As String = "debugTarget"
Private Const TARGET_TRACE As String = "traceTarget"
Private Const TARGET_JSON As String = "jsonTarget"
'Private Const TARGET_MEMORY As String = "memoryTarget"
Private Const LOG_FORMAT_BASE As String = "${time}|${logger:shortName=True}|${level:uppercase=true}"
@@ -101,8 +103,9 @@ Public Class LogConfig
Private ReadOnly _basePath As String = _failSafePath
Private _config As LoggingConfiguration
Private _isDebug As Boolean = False
Private _isTrace As Boolean = False
Private _EnableDebugLogging As Boolean = False
Private _EnableTraceLogging As Boolean = False
Private _EnableJsonLogging As Boolean = False
#End Region
#Region "Public Properties"
@@ -137,21 +140,31 @@ Public Class LogConfig
''' <returns>True, if debug log will be written. False otherwise.</returns>
Public Property Debug As Boolean
Get
Return _isDebug
Return _EnableDebugLogging
End Get
Set(isDebug As Boolean)
_isDebug = isDebug
ReloadConfig(isDebug, _isTrace)
Set(value As Boolean)
_EnableDebugLogging = value
ReloadConfig()
End Set
End Property
Public Property Trace As Boolean
Get
Return _isTrace
Return _EnableTraceLogging
End Get
Set(isTrace As Boolean)
_isTrace = isTrace
ReloadConfig(_isDebug, isTrace)
Set(value As Boolean)
_EnableTraceLogging = value
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 Property
@@ -443,6 +456,7 @@ Public Class LogConfig
_config.AddTarget(TARGET_DEFAULT, GetDefaultLogTarget(_basePath))
_config.AddTarget(TARGET_DEBUG, GetDebugLogTarget(_basePath))
_config.AddTarget(TARGET_TRACE, GetTraceLogTarget(_basePath))
_config.AddTarget(TARGET_JSON, GetJsonLogTarget(_basePath))
'_config.AddTarget(TARGET_MEMORY, GetMemoryDebugTarget())
' Add default rules
@@ -478,9 +492,7 @@ Public Class LogConfig
''' <summary>
''' Reconfigures and re-adds all loggers, optionally adding the debug rule.
''' </summary>
''' <param name="Debug">Adds the Debug rule if true.</param>
''' <param name="Trace">Adds the Trace rule if true.</param>
Private Sub ReloadConfig(Optional Debug As Boolean = False, Optional Trace As Boolean = False)
Private Sub ReloadConfig()
Dim oLogger = GetLogger()
' Clear Logging Rules
@@ -489,15 +501,22 @@ Public Class LogConfig
' Add default rules
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
If Debug = True Then
If _EnableDebugLogging = True Then
_config.AddRule(LogLevel.Debug, LogLevel.Error, TARGET_DEBUG)
oLogger.Info("DEBUG Logging is now Enabled.")
Else
oLogger.Debug("DEBUG Logging is now Disabled.")
End If
If Trace = True Then
' Add trace rule, if configured
If _EnableTraceLogging = True Then
_config.AddRule(LogLevel.Trace, LogLevel.Error, TARGET_TRACE)
End If
@@ -506,6 +525,29 @@ Public Class LogConfig
End Sub
#Region "Log Targets"
Private Function GetJsonLogTarget(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 jsonLog As New FileTarget() With {
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_JSON),
.Name = TARGET_JSON,
.Layout = oJsonLayout,
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
.ArchiveEvery = ARCHIVE_EVERY,
.KeepFileOpen = KEEP_FILES_OPEN,
.Encoding = Text.Encoding.Unicode
}
Return jsonLog
End Function
Private Function GetDefaultLogTarget(basePath As String) As FileTarget
Dim defaultLog As New FileTarget() With {
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT),
@@ -565,9 +607,9 @@ Public Class LogConfig
End Function
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),
.Name = TARGET_DEBUG,
.Name = TARGET_TRACE,
.Layout = LOG_FORMAT_DEBUG,
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEBUG_DETAIL,
.ArchiveEvery = ARCHIVE_EVERY,
@@ -578,18 +620,7 @@ Public Class LogConfig
.Encoding = Text.Encoding.Unicode
}
Return debugLog
Return traceLog
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 Class

View File

@@ -13,7 +13,7 @@ Imports System.Runtime.InteropServices
<Assembly: AssemblyCompany("Digital Data")>
<Assembly: AssemblyProduct("Modules.Logging")>
<Assembly: AssemblyCopyright("Copyright © 2022")>
<Assembly: AssemblyTrademark("2.6.0.0")>
<Assembly: AssemblyTrademark("2.6.2.0")>
<Assembly: ComVisible(False)>
@@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("2.6.0.0")>
<Assembly: AssemblyFileVersion("2.6.0.0")>
<Assembly: AssemblyVersion("2.6.2.0")>
<Assembly: AssemblyFileVersion("2.6.2.0")>