jj: Logging Version 0.0.0.3
This commit is contained in:
parent
513ee837fb
commit
b92100e401
@ -6,9 +6,9 @@ Imports NLog.Targets
|
|||||||
''' <summary>
|
''' <summary>
|
||||||
''' MODULE: LogConfig
|
''' 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:
|
''' DESCRIPTION: Module that writes file-logs to different locations:
|
||||||
''' local application data, the current directory or a custom path.
|
''' local application data, the current directory or a custom path.
|
||||||
@ -16,8 +16,8 @@ Imports NLog.Targets
|
|||||||
'''
|
'''
|
||||||
''' Three different logfiles will be generated:
|
''' Three different logfiles will be generated:
|
||||||
'''
|
'''
|
||||||
''' - Default: Warn, Error and Fatal Log Levels
|
''' - Default: Info and Warn Log Levels
|
||||||
''' - Detail: Info Log Level
|
''' - Error: Warn, Error and Fatal Log Level
|
||||||
''' - Debug: Debug Log Level
|
''' - Debug: Debug Log Level
|
||||||
'''
|
'''
|
||||||
'''
|
'''
|
||||||
@ -48,13 +48,15 @@ Imports NLog.Targets
|
|||||||
''' Debug, Boolean
|
''' Debug, Boolean
|
||||||
''' Determines if the debug log should be written.
|
''' Determines if the debug log should be written.
|
||||||
'''
|
'''
|
||||||
''' EXAMPLES: Class FooProgram
|
''' EXAMPLES: Imports DigitalData.Modules.Logging
|
||||||
''' Private Logger as NLog.Logger
|
'''
|
||||||
''' Private LogConfig as DigitalData.Modules.Logging.LogConfig
|
''' Class FooProgram
|
||||||
|
''' Private Logger as Logger
|
||||||
|
''' Private LogConfig as LogConfig
|
||||||
'''
|
'''
|
||||||
''' Public Sub New(LogFactory as NLog.LogFactory)
|
''' Public Sub New()
|
||||||
''' LogConfig = new DigitalData.Modules.Logging.LogConfig(args)
|
''' LogConfig = new LogConfig(args)
|
||||||
''' Logger = LogConfig.LogFactory.GetCurrentClassLogger()
|
''' Logger = LogConfig.GetLogger()
|
||||||
''' End Sub
|
''' End Sub
|
||||||
'''
|
'''
|
||||||
''' Public Sub Bar()
|
''' Public Sub Bar()
|
||||||
@ -65,8 +67,8 @@ Imports NLog.Targets
|
|||||||
''' Class FooLib
|
''' Class FooLib
|
||||||
''' Private Logger as NLog.Logger
|
''' Private Logger as NLog.Logger
|
||||||
'''
|
'''
|
||||||
''' Public Sub New(LogFactory as NLog.LogFactory)
|
''' Public Sub New(LogConfig as LogConfig)
|
||||||
''' Logger = LogFactory.GetCurrentClassLogger()
|
''' Logger = LogConfig.GetLogger()
|
||||||
''' End Sub
|
''' End Sub
|
||||||
'''
|
'''
|
||||||
''' Public Sub Bar()
|
''' 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 FILE_NAME_FORMAT_ERROR As String = "${shortdate}-${var:product}${var:suffix}-Error.log"
|
||||||
|
|
||||||
Private Const TARGET_DEFAULT As String = "defaultTarget"
|
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_DETAIL As String = "detailTarget"
|
||||||
Private Const TARGET_DEBUG As String = "debugTarget"
|
Private Const TARGET_DEBUG As String = "debugTarget"
|
||||||
|
|
||||||
@ -216,11 +219,30 @@ Public Class LogConfig
|
|||||||
.Configuration = config
|
.Configuration = config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dim l = LogFactory.GetCurrentClassLogger(Of Logger)()
|
||||||
|
|
||||||
|
|
||||||
' Save log paths for files/directory
|
' Save log paths for files/directory
|
||||||
LogDirectory = basePath
|
LogDirectory = basePath
|
||||||
LogFile = GetCurrentLogFilePath()
|
LogFile = GetCurrentLogFilePath()
|
||||||
End Sub
|
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>
|
''' <summary>
|
||||||
''' Returns the initial log configuration
|
''' Returns the initial log configuration
|
||||||
''' </summary>
|
''' </summary>
|
||||||
@ -233,20 +255,29 @@ Public Class LogConfig
|
|||||||
config.Variables("suffix") = logFileSuffix
|
config.Variables("suffix") = logFileSuffix
|
||||||
|
|
||||||
' Add default targets
|
' 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_DEFAULT, GetDefaultLogTarget(basePath))
|
||||||
config.AddTarget(TARGET_DETAIL, GetDetailLogTarget(basePath))
|
|
||||||
config.AddTarget(TARGET_DEBUG, GetDebugLogTarget(basePath))
|
config.AddTarget(TARGET_DEBUG, GetDebugLogTarget(basePath))
|
||||||
|
|
||||||
' Add default rules
|
' Add default rules
|
||||||
config.AddRuleForOneLevel(LogLevel.Error, TARGET_DEFAULT_EX)
|
AddDefaultRules(config)
|
||||||
config.AddRuleForOneLevel(LogLevel.Fatal, TARGET_DEFAULT_EX)
|
|
||||||
config.AddRuleForOneLevel(LogLevel.Warn, TARGET_DEFAULT)
|
|
||||||
config.AddRuleForOneLevel(LogLevel.Info, TARGET_DETAIL)
|
|
||||||
|
|
||||||
Return config
|
Return config
|
||||||
End Function
|
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>
|
''' <summary>
|
||||||
''' Returns the full path of the current default log file.
|
''' Returns the full path of the current default log file.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
@ -268,10 +299,7 @@ Public Class LogConfig
|
|||||||
config.LoggingRules.Clear()
|
config.LoggingRules.Clear()
|
||||||
|
|
||||||
' Add default rules
|
' Add default rules
|
||||||
config.AddRuleForOneLevel(LogLevel.Error, TARGET_DEFAULT_EX)
|
AddDefaultRules(config)
|
||||||
config.AddRuleForOneLevel(LogLevel.Fatal, TARGET_DEFAULT_EX)
|
|
||||||
config.AddRuleForOneLevel(LogLevel.Warn, TARGET_DEFAULT)
|
|
||||||
config.AddRuleForOneLevel(LogLevel.Info, TARGET_DETAIL)
|
|
||||||
|
|
||||||
' Add debug rule, if configured
|
' Add debug rule, if configured
|
||||||
If Debug Then
|
If Debug Then
|
||||||
@ -295,18 +323,32 @@ Public Class LogConfig
|
|||||||
|
|
||||||
Return defaultLog
|
Return defaultLog
|
||||||
End Function
|
End Function
|
||||||
Private Function GetDefaultLogTargetWithExceptions(basePath As String) As FileTarget
|
Private Function GetErrorExceptionLogTarget(basePath As String) As FileTarget
|
||||||
Dim defaultLogWithExceptionData As New FileTarget() With {
|
Dim errorLogWithExceptions As New FileTarget() With {
|
||||||
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT),
|
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_ERROR),
|
||||||
.Name = TARGET_DEFAULT_EX,
|
.Name = TARGET_ERROR_EX,
|
||||||
.Layout = LOG_FORMAT_EXCEPTION,
|
.Layout = LOG_FORMAT_EXCEPTION,
|
||||||
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
|
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
|
||||||
.ArchiveEvery = ARCHIVE_EVERY,
|
.ArchiveEvery = ARCHIVE_EVERY,
|
||||||
.KeepFileOpen = KEEP_FILES_OPEN
|
.KeepFileOpen = KEEP_FILES_OPEN
|
||||||
}
|
}
|
||||||
|
|
||||||
Return defaultLogWithExceptionData
|
Return errorLogWithExceptions
|
||||||
End Function
|
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
|
Private Function GetDetailLogTarget(basePath As String) As FileTarget
|
||||||
Dim detailLog As New FileTarget() With {
|
Dim detailLog As New FileTarget() With {
|
||||||
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DETAIL),
|
.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>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="LogConfig.vb" />
|
<Compile Include="LogConfig.vb" />
|
||||||
|
<Compile Include="Logger.vb" />
|
||||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||||
<Compile Include="My Project\Application.Designer.vb">
|
<Compile Include="My Project\Application.Designer.vb">
|
||||||
<AutoGen>True</AutoGen>
|
<AutoGen>True</AutoGen>
|
||||||
|
|||||||
@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
|
|||||||
' übernehmen, indem Sie "*" eingeben:
|
' übernehmen, indem Sie "*" eingeben:
|
||||||
' <Assembly: AssemblyVersion("1.0.*")>
|
' <Assembly: AssemblyVersion("1.0.*")>
|
||||||
|
|
||||||
<Assembly: AssemblyVersion("0.0.0.2")>
|
<Assembly: AssemblyVersion("0.0.0.3")>
|
||||||
<Assembly: AssemblyFileVersion("1.0.0.0")>
|
<Assembly: AssemblyFileVersion("1.0.0.0")>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user