Merge remote-tracking branch 'origin/master' into ZugferdService-Redesign

This commit is contained in:
Jonathan Jenne 2020-03-20 13:49:18 +01:00
commit 84dab7f5a6
3 changed files with 57 additions and 47 deletions

View File

@ -1,4 +1,5 @@
Imports System.IO
Imports System.Reflection
Imports NLog
Imports NLog.Config
Imports NLog.Targets
@ -14,33 +15,6 @@ Imports NLog.Targets
''' <dependencies>
''' NLog, >= 4.5.8
''' </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>
''' Imports DigitalData.Modules.Logging
'''
@ -81,6 +55,7 @@ Imports NLog.Targets
''' - NLOG_INTERNAL_LOG_FILE: ex. C:\Temp\Nlog_Internal.log
''' </remarks>
Public Class LogConfig
#Region "Private Properties"
Private Const KEEP_FILES_OPEN As Boolean = False
' MAX_ARCHIVES_FILES works like this (in version 4.5.8):
' 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_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 FOLDER_NAME_LOG = "Log"
Private ReadOnly failSafePath As String = Path.GetTempPath()
Private ReadOnly basePath As String = failSafePath
Private config As LoggingConfiguration
Private isDebug As Boolean = False
#End Region
#Region "Public Properties"
Public Enum PathType As Integer
AppData = 0
CurrentDirectory = 1
@ -173,24 +149,41 @@ Public Class LogConfig
End Get
End Property
Public ReadOnly Property NLogConfig As LoggingConfiguration
Get
Return config
End Get
End Property
#End Region
''' <summary>
''' Initializes a new LogConfig object with a logpath 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>
''' <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
''' <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="Suffix">If set to anything other than Nothing, extends the logfile name with this suffix.</param>
''' <param name="CompanyName">CompanyName is used to construct log-path in when LogPath is set to PathType:AppData</param>
''' <param name="ProductName">ProductName is used to construct log-path in when LogPath is set to PathType:AppData</param>
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)
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)
basePath = Path.Combine(appDataDir, CompanyName, ProductName, FOLDER_NAME_LOG)
Else 'Custom Path
basePath = customLogPath
basePath = CustomLogPath
End If
' 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
Dim logFileSuffix As String = String.Empty
If suffix IsNot Nothing AndAlso suffix.Count > 0 Then
logFileSuffix = $"-{suffix}"
If Suffix IsNot Nothing AndAlso Suffix.Count > 0 Then
logFileSuffix = $"-{Suffix}"
End If
' Create config object and initalize it
config = GetConfig(productName, logFileSuffix)
config = GetConfig(ProductName, logFileSuffix)
' Save config
LogFactory = New LogFactory With {
@ -236,10 +229,24 @@ Public Class LogConfig
LogFile = GetCurrentLogFilePath()
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>
''' Returns the Logger for the calling class
''' </summary>
''' <returns>An object of Logging.Logger</returns>
<DebuggerStepThrough()>
Public Function GetLogger() As Logger
Dim oClassName As String = GetClassFullName()
Return LogFactory.GetLogger(Of Logger)(oClassName)
@ -250,6 +257,7 @@ Public Class LogConfig
''' </summary>
''' <param name="ClassName">The name of the class the logger belongs to</param>
''' <returns>An object of Logging.Logger</returns>
<DebuggerStepThrough()>
Public Function GetLogger(ClassName As String) As Logger
Return LogFactory.GetLogger(Of Logger)(ClassName)
End Function
@ -268,6 +276,7 @@ Public Class LogConfig
''' </summary>
''' <returns>The fully qualified class name</returns>
''' <remarks>This method is very resource-intensive!</remarks>
<DebuggerStepThrough()>
Public Shared Function GetClassFullName() As String
Dim oFramesToSkip As Integer = 2
Dim oClassName As String = String.Empty
@ -437,7 +446,5 @@ Public Class LogConfig
Return memoryLog
End Function
#End Region
End Class

View File

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

View File

@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("0.0.1.1")>
<Assembly: AssemblyVersion("2.0.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")>