jj: Logging Version 0.0.0.3
This commit is contained in:
parent
513ee837fb
commit
b92100e401
@ -6,9 +6,9 @@ Imports NLog.Targets
|
||||
''' <summary>
|
||||
''' MODULE: LogConfig
|
||||
'''
|
||||
''' VERSION: 0.0.0.2
|
||||
''' VERSION: 0.0.0.3
|
||||
'''
|
||||
''' DATE: 27.08.2018
|
||||
''' DATE: 02.10.2018
|
||||
'''
|
||||
''' DESCRIPTION: Module that writes file-logs to different locations:
|
||||
''' local application data, the current directory or a custom path.
|
||||
@ -16,8 +16,8 @@ Imports NLog.Targets
|
||||
'''
|
||||
''' Three different logfiles will be generated:
|
||||
'''
|
||||
''' - Default: Warn, Error and Fatal Log Levels
|
||||
''' - Detail: Info Log Level
|
||||
''' - Default: Info and Warn Log Levels
|
||||
''' - Error: Warn, Error and Fatal Log Level
|
||||
''' - Debug: Debug Log Level
|
||||
'''
|
||||
'''
|
||||
@ -48,13 +48,15 @@ Imports NLog.Targets
|
||||
''' Debug, Boolean
|
||||
''' Determines if the debug log should be written.
|
||||
'''
|
||||
''' EXAMPLES: Class FooProgram
|
||||
''' Private Logger as NLog.Logger
|
||||
''' Private LogConfig as DigitalData.Modules.Logging.LogConfig
|
||||
''' EXAMPLES: Imports DigitalData.Modules.Logging
|
||||
'''
|
||||
''' Class FooProgram
|
||||
''' Private Logger as Logger
|
||||
''' Private LogConfig as LogConfig
|
||||
'''
|
||||
''' Public Sub New(LogFactory as NLog.LogFactory)
|
||||
''' LogConfig = new DigitalData.Modules.Logging.LogConfig(args)
|
||||
''' Logger = LogConfig.LogFactory.GetCurrentClassLogger()
|
||||
''' Public Sub New()
|
||||
''' LogConfig = new LogConfig(args)
|
||||
''' Logger = LogConfig.GetLogger()
|
||||
''' End Sub
|
||||
'''
|
||||
''' Public Sub Bar()
|
||||
@ -65,8 +67,8 @@ Imports NLog.Targets
|
||||
''' Class FooLib
|
||||
''' Private Logger as NLog.Logger
|
||||
'''
|
||||
''' Public Sub New(LogFactory as NLog.LogFactory)
|
||||
''' Logger = LogFactory.GetCurrentClassLogger()
|
||||
''' Public Sub New(LogConfig as LogConfig)
|
||||
''' Logger = LogConfig.GetLogger()
|
||||
''' End Sub
|
||||
'''
|
||||
''' Public Sub Bar()
|
||||
@ -99,7 +101,8 @@ Public Class LogConfig
|
||||
Private Const FILE_NAME_FORMAT_ERROR As String = "${shortdate}-${var:product}${var:suffix}-Error.log"
|
||||
|
||||
Private Const TARGET_DEFAULT As String = "defaultTarget"
|
||||
Private Const TARGET_DEFAULT_EX As String = "defaultExTarget"
|
||||
Private Const TARGET_ERROR_EX As String = "errorExceptionTarget"
|
||||
Private Const TARGET_ERROR As String = "errorTarget"
|
||||
Private Const TARGET_DETAIL As String = "detailTarget"
|
||||
Private Const TARGET_DEBUG As String = "debugTarget"
|
||||
|
||||
@ -216,11 +219,30 @@ Public Class LogConfig
|
||||
.Configuration = config
|
||||
}
|
||||
|
||||
Dim l = LogFactory.GetCurrentClassLogger(Of Logger)()
|
||||
|
||||
|
||||
' Save log paths for files/directory
|
||||
LogDirectory = basePath
|
||||
LogFile = GetCurrentLogFilePath()
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Returns the Logger for the calling class
|
||||
''' </summary>
|
||||
''' <returns>An object of Logging.Logger</returns>
|
||||
Public Function GetLogger() As Logger
|
||||
Dim name As String = "Unknown"
|
||||
Try
|
||||
Dim frame As New StackFrame(1)
|
||||
Dim method = frame.GetMethod()
|
||||
name = method.DeclaringType.FullName
|
||||
Catch ex As Exception
|
||||
End Try
|
||||
|
||||
Return LogFactory.GetLogger(Of Logger)(name)
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Returns the initial log configuration
|
||||
''' </summary>
|
||||
@ -233,20 +255,29 @@ Public Class LogConfig
|
||||
config.Variables("suffix") = logFileSuffix
|
||||
|
||||
' Add default targets
|
||||
config.AddTarget(TARGET_DEFAULT_EX, GetDefaultLogTargetWithExceptions(basePath))
|
||||
config.AddTarget(TARGET_ERROR_EX, GetErrorExceptionLogTarget(basePath))
|
||||
config.AddTarget(TARGET_ERROR, GetErrorLogTarget(basePath))
|
||||
config.AddTarget(TARGET_DEFAULT, GetDefaultLogTarget(basePath))
|
||||
config.AddTarget(TARGET_DETAIL, GetDetailLogTarget(basePath))
|
||||
config.AddTarget(TARGET_DEBUG, GetDebugLogTarget(basePath))
|
||||
|
||||
' Add default rules
|
||||
config.AddRuleForOneLevel(LogLevel.Error, TARGET_DEFAULT_EX)
|
||||
config.AddRuleForOneLevel(LogLevel.Fatal, TARGET_DEFAULT_EX)
|
||||
config.AddRuleForOneLevel(LogLevel.Warn, TARGET_DEFAULT)
|
||||
config.AddRuleForOneLevel(LogLevel.Info, TARGET_DETAIL)
|
||||
AddDefaultRules(config)
|
||||
|
||||
Return config
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
''' Adds the default rules
|
||||
''' </summary>
|
||||
''' <param name="config">A NLog.LoggingConfiguration object</param>
|
||||
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
|
||||
|
||||
''' <summary>
|
||||
''' Returns the full path of the current default log file.
|
||||
''' </summary>
|
||||
@ -268,10 +299,7 @@ Public Class LogConfig
|
||||
config.LoggingRules.Clear()
|
||||
|
||||
' Add default rules
|
||||
config.AddRuleForOneLevel(LogLevel.Error, TARGET_DEFAULT_EX)
|
||||
config.AddRuleForOneLevel(LogLevel.Fatal, TARGET_DEFAULT_EX)
|
||||
config.AddRuleForOneLevel(LogLevel.Warn, TARGET_DEFAULT)
|
||||
config.AddRuleForOneLevel(LogLevel.Info, TARGET_DETAIL)
|
||||
AddDefaultRules(config)
|
||||
|
||||
' Add debug rule, if configured
|
||||
If Debug Then
|
||||
@ -295,18 +323,32 @@ Public Class LogConfig
|
||||
|
||||
Return defaultLog
|
||||
End Function
|
||||
Private Function GetDefaultLogTargetWithExceptions(basePath As String) As FileTarget
|
||||
Dim defaultLogWithExceptionData As New FileTarget() With {
|
||||
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT),
|
||||
.Name = TARGET_DEFAULT_EX,
|
||||
Private Function GetErrorExceptionLogTarget(basePath As String) As FileTarget
|
||||
Dim errorLogWithExceptions As New FileTarget() With {
|
||||
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_ERROR),
|
||||
.Name = TARGET_ERROR_EX,
|
||||
.Layout = LOG_FORMAT_EXCEPTION,
|
||||
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
|
||||
.ArchiveEvery = ARCHIVE_EVERY,
|
||||
.KeepFileOpen = KEEP_FILES_OPEN
|
||||
}
|
||||
|
||||
Return defaultLogWithExceptionData
|
||||
Return errorLogWithExceptions
|
||||
End Function
|
||||
|
||||
Private Function GetErrorLogTarget(basePath As String) As FileTarget
|
||||
Dim errorLog As New FileTarget() With {
|
||||
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_ERROR),
|
||||
.Name = TARGET_ERROR,
|
||||
.Layout = LOG_FORMAT_DEFAULT,
|
||||
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
|
||||
.ArchiveEvery = ARCHIVE_EVERY,
|
||||
.KeepFileOpen = KEEP_FILES_OPEN
|
||||
}
|
||||
|
||||
Return errorLog
|
||||
End Function
|
||||
|
||||
Private Function GetDetailLogTarget(basePath As String) As FileTarget
|
||||
Dim detailLog As New FileTarget() With {
|
||||
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DETAIL),
|
||||
|
||||
17
Modules.Logging/Logger.vb
Normal file
17
Modules.Logging/Logger.vb
Normal file
@ -0,0 +1,17 @@
|
||||
Imports NLog
|
||||
|
||||
Public Class Logger
|
||||
Inherits NLog.Logger
|
||||
|
||||
''' <summary>
|
||||
''' Prints a preformatted Block including a block identifier
|
||||
''' </summary>
|
||||
''' <param name="blockId">A unique Identifier for this block, eg. DocId, FullPath, ..</param>
|
||||
Public Sub NewBlock(blockId As String)
|
||||
Dim message As String = $"=========={vbTab}{vbTab}Start of Block {blockId}{vbTab}{vbTab}=========="
|
||||
Dim logEventInfo As New LogEventInfo(LogLevel.Info, Name, message)
|
||||
Dim WrapperType As Type = GetType(Logger)
|
||||
|
||||
Log(WrapperType, logEventInfo)
|
||||
End Sub
|
||||
End Class
|
||||
@ -73,6 +73,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="LogConfig.vb" />
|
||||
<Compile Include="Logger.vb" />
|
||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||
<Compile Include="My Project\Application.Designer.vb">
|
||||
<AutoGen>True</AutoGen>
|
||||
|
||||
@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
|
||||
' übernehmen, indem Sie "*" eingeben:
|
||||
' <Assembly: AssemblyVersion("1.0.*")>
|
||||
|
||||
<Assembly: AssemblyVersion("0.0.0.2")>
|
||||
<Assembly: AssemblyVersion("0.0.0.3")>
|
||||
<Assembly: AssemblyFileVersion("1.0.0.0")>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user