Modules.Logging: Version 2.0.0.0

This commit is contained in:
Jonathan Jenne 2020-03-20 13:48:43 +01:00
parent a437359114
commit 3449b16d69
3 changed files with 57 additions and 47 deletions

View File

@ -1,4 +1,5 @@
Imports System.IO Imports System.IO
Imports System.Reflection
Imports NLog Imports NLog
Imports NLog.Config Imports NLog.Config
Imports NLog.Targets Imports NLog.Targets
@ -14,33 +15,6 @@ Imports NLog.Targets
''' <dependencies> ''' <dependencies>
''' NLog, >= 4.5.8 ''' NLog, >= 4.5.8
''' </dependencies> ''' </dependencies>
''' <params>
''' 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.
''' </params>
''' <props>
''' LogFile, String (readonly)
''' Returns the full path of the default log file.
'''
''' LogPath, String (readonly)
''' Returns the path to the log directory.
'''
''' LogFactory, NLog.LogFactory (readonly)
''' Returns the LogFactory that is used to create the Logger object
'''
''' Debug, Boolean
''' Determines if the debug log should be written.
''' </props>
''' <example> ''' <example>
''' Imports DigitalData.Modules.Logging ''' Imports DigitalData.Modules.Logging
''' '''
@ -81,6 +55,7 @@ Imports NLog.Targets
''' - NLOG_INTERNAL_LOG_FILE: ex. C:\Temp\Nlog_Internal.log ''' - NLOG_INTERNAL_LOG_FILE: ex. C:\Temp\Nlog_Internal.log
''' </remarks> ''' </remarks>
Public Class LogConfig Public Class LogConfig
#Region "Private Properties"
Private Const KEEP_FILES_OPEN As Boolean = False Private Const KEEP_FILES_OPEN As Boolean = False
' MAX_ARCHIVES_FILES works like this (in version 4.5.8): ' MAX_ARCHIVES_FILES works like this (in version 4.5.8):
' 0 = keep ALL archives files ' 0 = keep ALL archives files
@ -113,15 +88,16 @@ Public Class LogConfig
Private Const LOG_FORMAT_DEBUG As String = LOG_FORMAT_BASE_LONG_DATE & " >> ${message}" Private Const LOG_FORMAT_DEBUG As String = LOG_FORMAT_BASE_LONG_DATE & " >> ${message}"
Private Const LOG_FORMAT_MEMORY As String = LOG_FORMAT_BASE_LONG_DATE & " >> ${message}${newline}${exception:format=Message}${newline}${exception:format=StackTrace}" Private Const LOG_FORMAT_MEMORY As String = LOG_FORMAT_BASE_LONG_DATE & " >> ${message}${newline}${exception:format=Message}${newline}${exception:format=StackTrace}"
Private Const FOLDER_NAME_LOG = "Log"
Private Const FILE_NAME_ACCESS_TEST = "accessTest.txt" Private Const FILE_NAME_ACCESS_TEST = "accessTest.txt"
Private Const FOLDER_NAME_LOG = "Log"
Private ReadOnly failSafePath As String = Path.GetTempPath() Private ReadOnly failSafePath As String = Path.GetTempPath()
Private ReadOnly basePath As String = failSafePath Private ReadOnly basePath As String = failSafePath
Private config As LoggingConfiguration Private config As LoggingConfiguration
Private isDebug As Boolean = False Private isDebug As Boolean = False
#End Region
#Region "Public Properties"
Public Enum PathType As Integer Public Enum PathType As Integer
AppData = 0 AppData = 0
CurrentDirectory = 1 CurrentDirectory = 1
@ -173,24 +149,41 @@ Public Class LogConfig
End Get End Get
End Property End Property
Public ReadOnly Property NLogConfig As LoggingConfiguration
Get
Return config
End Get
End Property
#End Region
''' <summary> ''' <summary>
''' Initializes a new LogConfig object with a logpath and optinally a filename-suffix. ''' Initializes a new LogConfig object with a logpath and optinally a filename-suffix.
''' </summary> ''' </summary>
''' <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>
''' <param name="suffix">If set to anything other than Nothing, extends the logfile name with this suffix.</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) ''' <param name="CompanyName">CompanyName is used to construct log-path in when LogPath is set to PathType:AppData</param>
Dim productName As String = My.Application.Info.ProductName ''' <param name="ProductName">ProductName is used to construct log-path in when LogPath is set to PathType:AppData</param>
Dim companyName As String = My.Application.Info.CompanyName Public Sub New(LogPath As PathType,
Optional CustomLogPath As String = Nothing,
Optional Suffix As String = Nothing,
Optional CompanyName As String = Nothing,
Optional ProductName As String = Nothing)
If logPath = PathType.AppData Then If LogPath = PathType.AppData And (ProductName Is Nothing Or CompanyName Is Nothing) Then
Throw New ArgumentException("Modules.Logging: PathType is AppData and either CompanyName or ProductName was not supplied!")
End If
If LogPath = PathType.CurrentDirectory Then
Throw New ArgumentException("Modules.Logging: LogPath.CurrentDirectory is deprecated. Please use LogPath.CustomPath!")
End If
If LogPath = PathType.AppData Then
Dim appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) Dim appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
basePath = Path.Combine(appDataDir, companyName, productName, FOLDER_NAME_LOG) basePath = Path.Combine(appDataDir, CompanyName, ProductName, FOLDER_NAME_LOG)
ElseIf logPath = PathType.CurrentDirectory Then
Dim currentDirectory As String = My.Application.Info.DirectoryPath
basePath = Path.Combine(currentDirectory, FOLDER_NAME_LOG)
Else 'Custom Path Else 'Custom Path
basePath = customLogPath basePath = CustomLogPath
End If End If
' If directory does not exist, try to create it! ' If directory does not exist, try to create it!
@ -219,12 +212,12 @@ Public Class LogConfig
' Set the suffix to the given string if it exists ' Set the suffix to the given string if it exists
Dim logFileSuffix As String = String.Empty Dim logFileSuffix As String = String.Empty
If suffix IsNot Nothing AndAlso suffix.Count > 0 Then If Suffix IsNot Nothing AndAlso Suffix.Count > 0 Then
logFileSuffix = $"-{suffix}" logFileSuffix = $"-{Suffix}"
End If End If
' Create config object and initalize it ' Create config object and initalize it
config = GetConfig(productName, logFileSuffix) config = GetConfig(ProductName, logFileSuffix)
' Save config ' Save config
LogFactory = New LogFactory With { LogFactory = New LogFactory With {
@ -236,10 +229,24 @@ Public Class LogConfig
LogFile = GetCurrentLogFilePath() LogFile = GetCurrentLogFilePath()
End Sub End Sub
Private Sub CheckMyApplication()
Dim oAssembly = Assembly.GetEntryAssembly()
Dim oMyApp = Nothing
For Each oType As Type In oAssembly.DefinedTypes
If oType.Name = "MyApplication" Then
oMyApp = oType
Exit For
End If
Next
oMyApp.GetType().GetProperty("")
End Sub
''' <summary> ''' <summary>
''' Returns the Logger for the calling class ''' Returns the Logger for the calling class
''' </summary> ''' </summary>
''' <returns>An object of Logging.Logger</returns> ''' <returns>An object of Logging.Logger</returns>
<DebuggerStepThrough()>
Public Function GetLogger() As Logger Public Function GetLogger() As Logger
Dim oClassName As String = GetClassFullName() Dim oClassName As String = GetClassFullName()
Return LogFactory.GetLogger(Of Logger)(oClassName) Return LogFactory.GetLogger(Of Logger)(oClassName)
@ -250,6 +257,7 @@ Public Class LogConfig
''' </summary> ''' </summary>
''' <param name="ClassName">The name of the class the logger belongs to</param> ''' <param name="ClassName">The name of the class the logger belongs to</param>
''' <returns>An object of Logging.Logger</returns> ''' <returns>An object of Logging.Logger</returns>
<DebuggerStepThrough()>
Public Function GetLogger(ClassName As String) As Logger Public Function GetLogger(ClassName As String) As Logger
Return LogFactory.GetLogger(Of Logger)(ClassName) Return LogFactory.GetLogger(Of Logger)(ClassName)
End Function End Function
@ -268,6 +276,7 @@ Public Class LogConfig
''' </summary> ''' </summary>
''' <returns>The fully qualified class name</returns> ''' <returns>The fully qualified class name</returns>
''' <remarks>This method is very resource-intensive!</remarks> ''' <remarks>This method is very resource-intensive!</remarks>
<DebuggerStepThrough()>
Public Shared Function GetClassFullName() As String Public Shared Function GetClassFullName() As String
Dim oFramesToSkip As Integer = 2 Dim oFramesToSkip As Integer = 2
Dim oClassName As String = String.Empty Dim oClassName As String = String.Empty
@ -437,7 +446,5 @@ Public Class LogConfig
Return memoryLog Return memoryLog
End Function End Function
#End Region #End Region
End Class End Class

View File

@ -2,11 +2,13 @@
Public Class Logger Public Class Logger
Inherits NLog.Logger Inherits NLog.Logger
Implements ILogger
''' <summary> ''' <summary>
''' Prints a preformatted Block including a block identifier ''' Prints a preformatted Block including a block identifier
''' </summary> ''' </summary>
''' <param name="blockId">A unique Identifier for this block, eg. DocId, FullPath, ..</param> ''' <param name="blockId">A unique Identifier for this block, eg. DocId, FullPath, ..</param>
<DebuggerStepThrough()>
Public Sub NewBlock(blockId As String) Public Sub NewBlock(blockId As String)
Dim message As String = $"-----> Start of Block {blockId}" Dim message As String = $"-----> Start of Block {blockId}"
Dim logEventInfo As New LogEventInfo(LogLevel.Info, Name, message) Dim logEventInfo As New LogEventInfo(LogLevel.Info, Name, message)
@ -15,6 +17,7 @@ Public Class Logger
Log(WrapperType, logEventInfo) Log(WrapperType, logEventInfo)
End Sub End Sub
<DebuggerStepThrough()>
Public Sub EndBlock() Public Sub EndBlock()
Dim message As String = $"<----- End of Block" Dim message As String = $"<----- End of Block"
Dim logEventInfo As New LogEventInfo(LogLevel.Info, Name, message) Dim logEventInfo As New LogEventInfo(LogLevel.Info, Name, message)

View File

@ -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("0.0.1.1")> <Assembly: AssemblyVersion("2.0.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")> <Assembly: AssemblyFileVersion("1.0.0.0")>