|
|
|
|
@@ -1,5 +1,4 @@
|
|
|
|
|
Imports System.IO
|
|
|
|
|
|
|
|
|
|
Imports NLog
|
|
|
|
|
Imports NLog.Config
|
|
|
|
|
Imports NLog.Targets
|
|
|
|
|
@@ -19,8 +18,7 @@ Imports NLog.Targets
|
|
|
|
|
'''
|
|
|
|
|
''' DEPENDENCIES: NLog, >= 4.5.8
|
|
|
|
|
'''
|
|
|
|
|
''' PARAMETERS: product, String
|
|
|
|
|
''' logPath, PathType
|
|
|
|
|
''' PARAMETERS: logPath, PathType
|
|
|
|
|
''' customLogPath, String (optional)
|
|
|
|
|
'''
|
|
|
|
|
''' PROPERTIES: LogFile, String (readonly)
|
|
|
|
|
@@ -28,6 +26,11 @@ Imports NLog.Targets
|
|
|
|
|
''' Debug, Boolean
|
|
|
|
|
''' </summary>
|
|
|
|
|
Public Class Logger
|
|
|
|
|
Private Const KEEP_FILES_OPEN As Boolean = False
|
|
|
|
|
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.Minute
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
@@ -86,13 +89,15 @@ Public Class Logger
|
|
|
|
|
''' - CurrentDirectory: writes to `Log` directory relative to the current directory
|
|
|
|
|
''' - CustomPath: writes to custom path specified in `customLogPath`
|
|
|
|
|
''' </summary>
|
|
|
|
|
''' <param name="product">An name that identifies the Logs</param>
|
|
|
|
|
''' <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(product As String, logPath As PathType, Optional customLogPath As String = Nothing)
|
|
|
|
|
Public Sub New(logPath As PathType, Optional customLogPath As String = Nothing)
|
|
|
|
|
Dim productName As String = My.Application.Info.ProductName
|
|
|
|
|
Dim companyName As String = My.Application.Info.CompanyName
|
|
|
|
|
|
|
|
|
|
If logPath = PathType.AppData Then
|
|
|
|
|
Dim appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
|
|
|
|
|
basePath = Path.Combine(appDataDir, "Digital Data", product)
|
|
|
|
|
basePath = Path.Combine(appDataDir, companyName, productName)
|
|
|
|
|
ElseIf logPath = PathType.CurrentDirectory Then
|
|
|
|
|
Dim currentDirectory As String = Environment.CurrentDirectory
|
|
|
|
|
basePath = Path.Combine(currentDirectory, "Log")
|
|
|
|
|
@@ -100,20 +105,28 @@ Public Class Logger
|
|
|
|
|
basePath = customLogPath
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
' Create config object and initalize it
|
|
|
|
|
config = New LoggingConfiguration()
|
|
|
|
|
config.Variables("product") = product
|
|
|
|
|
config.Variables("product") = productName
|
|
|
|
|
|
|
|
|
|
' Add default targets
|
|
|
|
|
config.AddTarget(TARGET_DEFAULT_EX, GetDefaultLogTargetWithExceptions(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)
|
|
|
|
|
|
|
|
|
|
' Save config
|
|
|
|
|
LogManager.Configuration = config
|
|
|
|
|
|
|
|
|
|
' Save log paths for files/directory
|
|
|
|
|
LogDirectory = basePath
|
|
|
|
|
LogFile = GetCurrentLogFilePath()
|
|
|
|
|
|
|
|
|
|
AddDefaultLogTargets()
|
|
|
|
|
|
|
|
|
|
LogManager.Configuration = config
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
@@ -137,7 +150,10 @@ Public Class Logger
|
|
|
|
|
config.LoggingRules.Clear()
|
|
|
|
|
|
|
|
|
|
' Add default targets and debug target
|
|
|
|
|
AddDefaultLogTargets()
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
If Debug Then
|
|
|
|
|
config.AddRuleForOneLevel(LogLevel.Debug, TARGET_DEBUG)
|
|
|
|
|
@@ -147,63 +163,52 @@ Public Class Logger
|
|
|
|
|
LogManager.ReconfigExistingLoggers()
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
''' Adds default rules
|
|
|
|
|
''' </summary>
|
|
|
|
|
Private Sub AddDefaultLogTargets()
|
|
|
|
|
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)
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
#Region "Log Targets"
|
|
|
|
|
Private Function GetDefaultLogTarget(basePath As String) As FileTarget
|
|
|
|
|
Dim defaultFilePath As String = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT)
|
|
|
|
|
Dim defaultLog As New FileTarget() With {
|
|
|
|
|
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT),
|
|
|
|
|
.Name = TARGET_DEFAULT,
|
|
|
|
|
.FileName = defaultFilePath,
|
|
|
|
|
.Layout = LOG_FORMAT_DEFAULT,
|
|
|
|
|
.MaxArchiveFiles = 30,
|
|
|
|
|
.KeepFileOpen = Constants.KEEP_FILES_OPEN
|
|
|
|
|
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
|
|
|
|
|
.ArchiveEvery = ARCHIVE_EVERY,
|
|
|
|
|
.KeepFileOpen = KEEP_FILES_OPEN
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Return defaultLog
|
|
|
|
|
End Function
|
|
|
|
|
Private Function GetDefaultLogTargetWithExceptions(basePath As String) As FileTarget
|
|
|
|
|
Dim defaultFilePath As String = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT)
|
|
|
|
|
Dim defaultLogWithExceptionData As New FileTarget() With {
|
|
|
|
|
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEFAULT),
|
|
|
|
|
.Name = TARGET_DEFAULT_EX,
|
|
|
|
|
.FileName = defaultFilePath,
|
|
|
|
|
.Layout = LOG_FORMAT_EXCEPTION,
|
|
|
|
|
.MaxArchiveFiles = 30,
|
|
|
|
|
.KeepFileOpen = Constants.KEEP_FILES_OPEN
|
|
|
|
|
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEFAULT,
|
|
|
|
|
.ArchiveEvery = ARCHIVE_EVERY,
|
|
|
|
|
.KeepFileOpen = KEEP_FILES_OPEN
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Return defaultLogWithExceptionData
|
|
|
|
|
End Function
|
|
|
|
|
Private Function GetDetailLogTarget(basePath As String) As FileTarget
|
|
|
|
|
Dim detailFilePath As String = Path.Combine(basePath, FILE_NAME_FORMAT_DETAIL)
|
|
|
|
|
Dim detailLog As New FileTarget() With {
|
|
|
|
|
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DETAIL),
|
|
|
|
|
.Name = TARGET_DETAIL,
|
|
|
|
|
.FileName = detailFilePath,
|
|
|
|
|
.Layout = LOG_FORMAT_DEFAULT,
|
|
|
|
|
.MaxArchiveFiles = 1,
|
|
|
|
|
.KeepFileOpen = Constants.KEEP_FILES_OPEN
|
|
|
|
|
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEBUG_DETAIL,
|
|
|
|
|
.ArchiveEvery = ARCHIVE_EVERY,
|
|
|
|
|
.KeepFileOpen = KEEP_FILES_OPEN
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Return detailLog
|
|
|
|
|
End Function
|
|
|
|
|
Private Function GetDebugLogTarget(basePath As String) As FileTarget
|
|
|
|
|
Dim debugFilePath As String = Path.Combine(basePath, FILE_NAME_FORMAT_DEBUG)
|
|
|
|
|
Dim debugLog As New FileTarget() With {
|
|
|
|
|
.FileName = Path.Combine(basePath, FILE_NAME_FORMAT_DEBUG),
|
|
|
|
|
.Name = TARGET_DEBUG,
|
|
|
|
|
.FileName = debugFilePath,
|
|
|
|
|
.Layout = LOG_FORMAT_DEFAULT,
|
|
|
|
|
.MaxArchiveFiles = 1,
|
|
|
|
|
.KeepFileOpen = Constants.KEEP_FILES_OPEN
|
|
|
|
|
.MaxArchiveFiles = MAX_ARCHIVE_FILES_DEBUG_DETAIL,
|
|
|
|
|
.ArchiveEvery = ARCHIVE_EVERY,
|
|
|
|
|
.KeepFileOpen = KEEP_FILES_OPEN
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Return debugLog
|
|
|
|
|
|