jj: remove product paramter, general tweaks, remove constants.vb

This commit is contained in:
Jonathan Jenne 2018-08-15 14:39:58 +02:00
parent 7527d59b93
commit 0e7863c2b2
4 changed files with 46 additions and 50 deletions

View File

@ -1,8 +0,0 @@
''' <summary>
''' Keeps the constants needed for the Logger
''' </summary>
Public Class Constants
Public Const KEEP_FILES_OPEN As Boolean = False
Public Const MAX_ARCHIVE_FILES_DEFAULT As Integer = 30
Public Const MAX_ARCHIVE_FILES_DEBUG_DETAIL As Integer = 1
End Class

View File

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

View File

@ -72,7 +72,6 @@
<Import Include="System.Threading.Tasks" /> <Import Include="System.Threading.Tasks" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Constants.vb" />
<Compile Include="Logger.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">

View File

@ -10,7 +10,7 @@ Imports System.Runtime.InteropServices
<Assembly: AssemblyTitle("Modules.Logging")> <Assembly: AssemblyTitle("Modules.Logging")>
<Assembly: AssemblyDescription("")> <Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")> <Assembly: AssemblyCompany("Digital Data")>
<Assembly: AssemblyProduct("Modules.Logging")> <Assembly: AssemblyProduct("Modules.Logging")>
<Assembly: AssemblyCopyright("Copyright © 2018")> <Assembly: AssemblyCopyright("Copyright © 2018")>
<Assembly: AssemblyTrademark("")> <Assembly: AssemblyTrademark("")>
@ -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("1.0.0.0")> <Assembly: AssemblyVersion("0.0.0.1")>
<Assembly: AssemblyFileVersion("1.0.0.0")> <Assembly: AssemblyFileVersion("1.0.0.0")>