diff --git a/Modules.Logging/Logger.vb b/Modules.Logging/Logger.vb index fb1f36a2..b901dbd0 100644 --- a/Modules.Logging/Logger.vb +++ b/Modules.Logging/Logger.vb @@ -100,12 +100,36 @@ Public Class Logger Dim appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) basePath = Path.Combine(appDataDir, companyName, productName) ElseIf logPath = PathType.CurrentDirectory Then - Dim currentDirectory As String = Environment.CurrentDirectory + Dim currentDirectory As String = My.Application.Info.DirectoryPath + basePath = Path.Combine(currentDirectory, "Log") Else 'Custom Path basePath = customLogPath End If + ' If directory does not exist, try to create it! + If Not Directory.Exists(basePath) Then + Try + Directory.CreateDirectory(basePath) + Catch ex As Exception + ' If creation fails, use failSafe path + basePath = failSafePath + End Try + End If + + ' Try to create a file in `basePath` to check write permissions + Try + Dim fileAccessPath = Path.Combine(basePath, "accessTest.txt") + Using fs As FileStream = File.Create(fileAccessPath) + fs.WriteByte(0) + End Using + + File.Delete(fileAccessPath) + Catch ex As UnauthorizedAccessException + ' If creation fails, use failSafe path + basePath = failSafePath + End Try + ' Create config object and initalize it config = New LoggingConfiguration() config.Variables("product") = productName