From 3449b16d69a566f05a6ae17c94402e92cd2d35e4 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Fri, 20 Mar 2020 13:48:43 +0100 Subject: [PATCH] Modules.Logging: Version 2.0.0.0 --- Modules.Logging/LogConfig.vb | 101 +++++++++++---------- Modules.Logging/Logger.vb | 3 + Modules.Logging/My Project/AssemblyInfo.vb | 2 +- 3 files changed, 58 insertions(+), 48 deletions(-) diff --git a/Modules.Logging/LogConfig.vb b/Modules.Logging/LogConfig.vb index c9e5e25d..3b4d9e99 100644 --- a/Modules.Logging/LogConfig.vb +++ b/Modules.Logging/LogConfig.vb @@ -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 ''' ''' NLog, >= 4.5.8 ''' -''' -''' 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. -''' -''' -''' 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. -''' ''' ''' Imports DigitalData.Modules.Logging ''' @@ -81,6 +55,7 @@ Imports NLog.Targets ''' - NLOG_INTERNAL_LOG_FILE: ex. C:\Temp\Nlog_Internal.log ''' 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 + ''' ''' Initializes a new LogConfig object with a logpath and optinally a filename-suffix. ''' - ''' The basepath to write logs to. Can be AppData, CurrentDirectory or CustomPath. - ''' If `logPath` is set to custom, this defines the custom logPath. - ''' If set to anything other than Nothing, extends the logfile name with this suffix. - 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 - - If logPath = PathType.AppData Then + ''' The basepath to write logs to. Can be AppData, CurrentDirectory or CustomPath. + ''' If `logPath` is set to custom, this defines the custom logPath. + ''' If set to anything other than Nothing, extends the logfile name with this suffix. + ''' CompanyName is used to construct log-path in when LogPath is set to PathType:AppData + ''' ProductName is used to construct log-path in when LogPath is set to PathType:AppData + 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 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 + ''' ''' Returns the Logger for the calling class ''' ''' An object of Logging.Logger + Public Function GetLogger() As Logger Dim oClassName As String = GetClassFullName() Return LogFactory.GetLogger(Of Logger)(oClassName) @@ -250,6 +257,7 @@ Public Class LogConfig ''' ''' The name of the class the logger belongs to ''' An object of Logging.Logger + Public Function GetLogger(ClassName As String) As Logger Return LogFactory.GetLogger(Of Logger)(ClassName) End Function @@ -268,6 +276,7 @@ Public Class LogConfig ''' ''' The fully qualified class name ''' This method is very resource-intensive! + 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 diff --git a/Modules.Logging/Logger.vb b/Modules.Logging/Logger.vb index f908714d..abf4be24 100644 --- a/Modules.Logging/Logger.vb +++ b/Modules.Logging/Logger.vb @@ -2,11 +2,13 @@ Public Class Logger Inherits NLog.Logger + Implements ILogger ''' ''' Prints a preformatted Block including a block identifier ''' ''' A unique Identifier for this block, eg. DocId, FullPath, .. + 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 + Public Sub EndBlock() Dim message As String = $"<----- End of Block" Dim logEventInfo As New LogEventInfo(LogLevel.Info, Name, message) diff --git a/Modules.Logging/My Project/AssemblyInfo.vb b/Modules.Logging/My Project/AssemblyInfo.vb index 3f292b18..d18ba061 100644 --- a/Modules.Logging/My Project/AssemblyInfo.vb +++ b/Modules.Logging/My Project/AssemblyInfo.vb @@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices ' übernehmen, indem Sie "*" eingeben: ' - +