180 lines
6.4 KiB
VB.net
180 lines
6.4 KiB
VB.net
Imports NLog
|
|
Imports NLog.Config
|
|
Imports NLog.Targets
|
|
Imports NLog.Targets.Wrappers
|
|
Imports NLog.Targets.Wrappers.BufferingTargetWrapperOverflowAction
|
|
|
|
Imports System.IO
|
|
|
|
Public Class ClassLogger
|
|
Private Const FileNameFormat As String = "${shortdate}-${var:product}.log"
|
|
Private Const FileNameFormatDetail As String = "${shortdate}-${var:product}-Detail.log"
|
|
Private Const FileNameFormatDebug As String = "${shortdate}-${var:product}-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 defaultLogFormat As String = "${longdate}|${level:uppercase=true}|${logger}|${message}"
|
|
Private Const exceptionLogFormat As String = "${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=message}|${exception:format=toString}"
|
|
|
|
Private config As LoggingConfiguration
|
|
Private debugOn As Boolean = False
|
|
|
|
Private ReadOnly failSafePath As String = Path.GetTempPath()
|
|
Private ReadOnly basePath As String = failSafePath
|
|
|
|
Public Enum PathType As Integer
|
|
AppData = 0
|
|
CurrentDirectory = 1
|
|
CustomPath = 2
|
|
End Enum
|
|
|
|
Public LogFile As String
|
|
|
|
|
|
Public Property Debug As Boolean
|
|
Get
|
|
Return debugOn
|
|
End Get
|
|
Set(value As Boolean)
|
|
If debugOn <> value Then
|
|
debugOn = value
|
|
|
|
If value = True Then
|
|
ActivateDebugLog()
|
|
Else
|
|
DeactivateDebugLog()
|
|
End If
|
|
End If
|
|
End Set
|
|
End Property
|
|
|
|
''' <summary>
|
|
''' Initializes a new Logger for a specific "Product"
|
|
''' </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)
|
|
If logPath = PathType.AppData Then
|
|
Dim appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
|
|
basePath = Path.Combine(appDataDir, "Digital Data", product)
|
|
ElseIf logPath = PathType.CurrentDirectory Then
|
|
basePath = "Log"
|
|
Else 'Custom Path
|
|
basePath = customLogPath
|
|
End If
|
|
|
|
config = New LoggingConfiguration()
|
|
config.Variables("product") = product
|
|
|
|
config.AddTarget(TARGET_DEFAULT_EX, WrapTargetInBuffered(GetDefaultLogTargetWithExceptions(basePath)))
|
|
config.AddTarget(TARGET_DEFAULT, WrapTargetInBuffered(GetDefaultLogTarget(basePath)))
|
|
config.AddTarget(TARGET_DETAIL, WrapTargetInBuffered(GetDetailLogTarget(basePath)))
|
|
config.AddTarget(TARGET_DEBUG, WrapTargetInBuffered(GetDebugLogTarget(basePath)))
|
|
|
|
AddDefaultLogTargets()
|
|
|
|
LogManager.Configuration = config
|
|
End Sub
|
|
|
|
Private Function WrapTargetInBuffered(target As FileTarget) As Target
|
|
Dim bufferedWrapper = New BufferingTargetWrapper()
|
|
|
|
bufferedWrapper.WrappedTarget = target
|
|
|
|
' Number of log events to be buffered. When the limit is reached,
|
|
' then a synchronous flush is performed.
|
|
bufferedWrapper.BufferSize = 100
|
|
|
|
' Action to be taken when the buffer exceeds the set bufferSize.
|
|
bufferedWrapper.OverflowAction = Flush
|
|
|
|
' Timeout (in milliseconds) after a write, until the entire buffer
|
|
' is asynchronously flushed. Use -1 to disable timed flushes.
|
|
bufferedWrapper.FlushTimeout = 100
|
|
|
|
Return bufferedWrapper
|
|
End Function
|
|
|
|
|
|
Private Sub ActivateDebugLog()
|
|
' Clear Logging Rules
|
|
config.LoggingRules.Clear()
|
|
|
|
' Add default targets and debug target
|
|
AddDefaultLogTargets()
|
|
config.AddRuleForOneLevel(LogLevel.Debug, TARGET_DEBUG)
|
|
|
|
' Reload all running loggers
|
|
LogManager.ReconfigExistingLoggers()
|
|
End Sub
|
|
|
|
Private Sub DeactivateDebugLog()
|
|
' Clear Logging Rules
|
|
config.LoggingRules.Clear()
|
|
|
|
' Add default targets
|
|
AddDefaultLogTargets()
|
|
|
|
' Reload all running loggers
|
|
LogManager.ReconfigExistingLoggers()
|
|
End Sub
|
|
|
|
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, FileNameFormat)
|
|
Dim defaultLog As New FileTarget() With {
|
|
.FileName = defaultFilePath,
|
|
.Layout = defaultLogFormat,
|
|
.MaxArchiveFiles = 30,
|
|
.KeepFileOpen = True
|
|
}
|
|
|
|
Return defaultLog
|
|
End Function
|
|
Private Function GetDefaultLogTargetWithExceptions(basePath As String) As FileTarget
|
|
Dim defaultFilePath As String = Path.Combine(basePath, FileNameFormat)
|
|
Dim defaultLogWithExceptionData As New FileTarget() With {
|
|
.FileName = defaultFilePath,
|
|
.Layout = exceptionLogFormat,
|
|
.MaxArchiveFiles = 30,
|
|
.KeepFileOpen = True
|
|
}
|
|
|
|
Return defaultLogWithExceptionData
|
|
End Function
|
|
Private Function GetDetailLogTarget(basePath As String) As FileTarget
|
|
Dim detailFilePath As String = Path.Combine(basePath, FileNameFormatDetail)
|
|
Dim detailLog As New FileTarget() With {
|
|
.FileName = detailFilePath,
|
|
.Layout = defaultLogFormat,
|
|
.MaxArchiveFiles = 1,
|
|
.KeepFileOpen = True
|
|
}
|
|
|
|
Return detailLog
|
|
End Function
|
|
Private Function GetDebugLogTarget(basePath As String) As FileTarget
|
|
Dim debugFilePath As String = Path.Combine(basePath, FileNameFormatDebug)
|
|
Dim debugLog As New FileTarget() With {
|
|
.FileName = debugFilePath,
|
|
.Layout = defaultLogFormat,
|
|
.MaxArchiveFiles = 1,
|
|
.KeepFileOpen = True
|
|
}
|
|
|
|
Return debugLog
|
|
End Function
|
|
#End Region
|
|
End Class
|