jj: add suffix parameter

This commit is contained in:
Jonathan Jenne 2018-08-21 10:34:06 +02:00
parent a3fba73ab1
commit 0bca3a2afa

View File

@ -8,7 +8,7 @@ Imports NLog.Targets
'''
''' VERSION: 0.0.0.1
'''
''' DATE: 17.08.2018
''' DATE: 20.08.2018
'''
''' DESCRIPTION: Module that writes file-logs to different locations:
''' local application data, the current directory or a custom path.
@ -19,26 +19,48 @@ Imports NLog.Targets
''' DEPENDENCIES: NLog, >= 4.5.8
'''
''' PARAMETERS: logPath, PathType
''' The basepath to write logs to. Can be AppData, CurrentDirectory or CustomPath.
'''
''' - AppData: writes to local application data directory
''' - CurrentDirectory: writes to `Log` directory relative to the current directory
''' - CustomPath: writes to custom path specified in `customLogPath`
'''
''' customLogPath, String (optional)
''' If `logPath` is set to custom, this defines the custom logPath.
'''
''' suffix, String (optional)
''' If set to anything other than Nothing, extends the logfile name with this suffix.
'''
''' PROPERTIES: LogFile, String (readonly)
''' Contains the full path of the default log file.
'''
''' LogPath, String (readonly)
''' Contains the path to the log directory.
'''
''' Debug, Boolean
''' Determines if the debug log should be written.
'''
''' REMARKS: If logpath can not be written to, falls back to temp folder as defined in:
''' https://docs.microsoft.com/de-de/dotnet/api/system.io.path.gettemppath?view=netframework-4.7.2
''' </summary>
Public Class LogConfig
Private Const KEEP_FILES_OPEN As Boolean = False
' MAX_ARCHIVES_FILES works like this (in version 4.5.8):
' 0 = keep ALL archives files
' 1 = only keep latest logfile and NO archive files
' n = keep n archive files
Private Const MAX_ARCHIVE_FILES_DEFAULT As Integer = 30
Private Const MAX_ARCHIVE_FILES_DEBUG_DETAIL As Integer = 1
Private Const ARCHIVE_EVERY As FileArchivePeriod = FileArchivePeriod.Day
Private Const FILE_NAME_FORMAT_DEFAULT As String = "${shortdate}-${var:product}.log"
Private Const FILE_NAME_FORMAT_DETAIL As String = "${shortdate}-${var:product}-Detail.log"
Private Const FILE_NAME_FORMAT_DEBUG As String = "${shortdate}-${var:product}-Debug.log"
Private Const FILE_NAME_FORMAT_DEFAULT As String = "${shortdate}-${var:product}${var:suffix}.log"
Private Const FILE_NAME_FORMAT_DETAIL As String = "${shortdate}-${var:product}${var:suffix}-Detail.log"
Private Const FILE_NAME_FORMAT_DEBUG As String = "${shortdate}-${var:product}${var:suffix}-Debug.log"
Private Const TARGET_DEFAULT As String = "default"
Private Const TARGET_DEFAULT_EX As String = "defaultEx"
Private Const TARGET_DETAIL As String = "detail"
Private Const TARGET_DEBUG As String = "debug"
Private Const TARGET_DEFAULT As String = "defaultTarget"
Private Const TARGET_DEFAULT_EX As String = "defaultExTarget"
Private Const TARGET_DETAIL As String = "detailTarget"
Private Const TARGET_DEBUG As String = "debugTarget"
Private Const LOG_FORMAT_BASE As String = "${longdate}|${logger}|${level:uppercase=true}"
Private Const LOG_FORMAT_DEFAULT As String = LOG_FORMAT_BASE & " >> ${message}"
@ -83,16 +105,12 @@ Public Class LogConfig
End Property
''' <summary>
''' Initializes a new Logger for a specific `Product`.
'''
''' logPath configurations:
''' - AppData: writes to local application data directory
''' - CurrentDirectory: writes to `Log` directory relative to the current directory
''' - CustomPath: writes to custom path specified in `customLogPath`
''' Initializes a new Logger for a specific `Product` and optinally a filename-suffix.
''' </summary>
''' <param name="logPath">The basepath to write logs to. Can be AppData, CurrentDirectory or CustomPath.</param>
''' <param name="customLogPath">If `logPath` is set to custom, this defines the custom logPath</param>
Public Sub New(logPath As PathType, Optional customLogPath As String = Nothing)
''' <param name="customLogPath">If `logPath` is set to custom, this defines the custom logPath.</param>
''' <param name="suffix">If set to anything other than Nothing, extends the logfile name with this suffix.</param>
Public Sub New(logPath As PathType, Optional customLogPath As String = Nothing, Optional suffix As String = Nothing)
Dim productName As String = My.Application.Info.ProductName
Dim companyName As String = My.Application.Info.CompanyName
@ -130,9 +148,17 @@ Public Class LogConfig
basePath = failSafePath
End Try
' Set the suffix to the given string if it exists
Dim logFileSuffix As String = String.Empty
If suffix IsNot Nothing AndAlso suffix.Count > 0 Then
logFileSuffix = $"-{suffix}"
End If
' Create config object and initalize it
config = New LoggingConfiguration()
config.Variables("product") = productName
config.Variables("suffix") = logFileSuffix
' Add default targets
config.AddTarget(TARGET_DEFAULT_EX, GetDefaultLogTargetWithExceptions(basePath))
@ -188,7 +214,6 @@ Public Class LogConfig
LogManager.ReconfigExistingLoggers()
End Sub
#Region "Log Targets"
Private Function GetDefaultLogTarget(basePath As String) As FileTarget
Dim defaultLog As New FileTarget() With {