diff --git a/Logging/LogConfig.vb b/Logging/LogConfig.vb index 8a9d1458..091a7b33 100644 --- a/Logging/LogConfig.vb +++ b/Logging/LogConfig.vb @@ -81,6 +81,7 @@ Public Class LogConfig 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 +102,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 +139,31 @@ Public Class LogConfig ''' True, if debug log will be written. False otherwise. 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 @@ -478,9 +490,7 @@ Public Class LogConfig ''' ''' Reconfigures and re-adds all loggers, optionally adding the debug rule. ''' - ''' Adds the Debug rule if true. - ''' Adds the Trace rule if true. - Private Sub ReloadConfig(Optional Debug As Boolean = False, Optional Trace As Boolean = False) + Private Sub ReloadConfig() Dim oLogger = GetLogger() ' Clear Logging Rules @@ -489,15 +499,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,11 +523,34 @@ Public Class LogConfig End Sub #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 + 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 { .FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT), .Name = TARGET_DEFAULT, - .Layout = LOG_FORMAT_DEFAULT, + .Layout = oJsonLayout, .MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT, .ArchiveEvery = ARCHIVE_EVERY, .KeepFileOpen = KEEP_FILES_OPEN, @@ -565,9 +605,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 +618,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