From b92100e401bba270bd6899043ef0461c9739424a Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Mon, 8 Oct 2018 16:42:26 +0200 Subject: [PATCH] jj: Logging Version 0.0.0.3 --- Modules.Logging/LogConfig.vb | 98 +++++++++++++++------- Modules.Logging/Logger.vb | 17 ++++ Modules.Logging/Logging.vbproj | 1 + Modules.Logging/My Project/AssemblyInfo.vb | 2 +- 4 files changed, 89 insertions(+), 29 deletions(-) create mode 100644 Modules.Logging/Logger.vb diff --git a/Modules.Logging/LogConfig.vb b/Modules.Logging/LogConfig.vb index a97e9800..a0c4b0ee 100644 --- a/Modules.Logging/LogConfig.vb +++ b/Modules.Logging/LogConfig.vb @@ -6,9 +6,9 @@ Imports NLog.Targets ''' ''' 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 + ''' + ''' Returns the Logger for the calling class + ''' + ''' An object of Logging.Logger + 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 + ''' ''' Returns the initial log configuration ''' @@ -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 + ''' + ''' Adds the default rules + ''' + ''' A NLog.LoggingConfiguration object + 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 + ''' ''' Returns the full path of the current default log file. ''' @@ -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), diff --git a/Modules.Logging/Logger.vb b/Modules.Logging/Logger.vb new file mode 100644 index 00000000..3360b653 --- /dev/null +++ b/Modules.Logging/Logger.vb @@ -0,0 +1,17 @@ +Imports NLog + +Public Class Logger + Inherits NLog.Logger + + ''' + ''' Prints a preformatted Block including a block identifier + ''' + ''' A unique Identifier for this block, eg. DocId, FullPath, .. + 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 diff --git a/Modules.Logging/Logging.vbproj b/Modules.Logging/Logging.vbproj index 8d3112c4..111738f6 100644 --- a/Modules.Logging/Logging.vbproj +++ b/Modules.Logging/Logging.vbproj @@ -73,6 +73,7 @@ + True diff --git a/Modules.Logging/My Project/AssemblyInfo.vb b/Modules.Logging/My Project/AssemblyInfo.vb index a8f0cc9d..0744f0df 100644 --- a/Modules.Logging/My Project/AssemblyInfo.vb +++ b/Modules.Logging/My Project/AssemblyInfo.vb @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices ' übernehmen, indem Sie "*" eingeben: ' - +