From cedf28199eac3fa6fbdc76f5781bf93e254b53eb Mon Sep 17 00:00:00 2001 From: sonderau Date: Wed, 29 Oct 2025 08:17:43 +0800 Subject: [PATCH] Fix: Use AppData directory for log files to avoid permission errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Issue: Application crashed when installed in Program Files due to UnauthorizedAccessException when trying to write log files to the installation directory. Changes: - SystemInfo.cs: Updated error logging to use LocalApplicationData/PSG-Oversight - OsqueryService.cs: Added GetLogPath() helper method and updated all log writes to use user's AppData directory instead of current directory - Added try-catch wrappers to silently handle any remaining logging failures All log files now write to: %LOCALAPPDATA%\PSG-Oversight\ This fixes the startup crash reported in Event Viewer when running the installed application. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- LD-SysInfo/Services/OsqueryService.cs | 29 ++++++++++++++++++++++----- LD-SysInfo/SystemInfo.cs | 12 ++++++++++- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/LD-SysInfo/Services/OsqueryService.cs b/LD-SysInfo/Services/OsqueryService.cs index 9b4a885..1a7e66b 100644 --- a/LD-SysInfo/Services/OsqueryService.cs +++ b/LD-SysInfo/Services/OsqueryService.cs @@ -9,6 +9,13 @@ namespace LD_SysInfo.Services { public static class OsqueryService { + private static string GetLogPath(string filename) + { + string logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PSG-Oversight"); + Directory.CreateDirectory(logDir); + return Path.Combine(logDir, filename); + } + private static string GetOsqueryPath() { var baseDir = AppContext.BaseDirectory; @@ -16,7 +23,11 @@ namespace LD_SysInfo.Services if (!File.Exists(path)) { - File.AppendAllText("osquery_error.log", $"[{DateTime.Now}] ❌ osqueryi.exe not found at {path}\n"); + try + { + File.AppendAllText(GetLogPath("osquery_error.log"), $"[{DateTime.Now}] ❌ osqueryi.exe not found at {path}\n"); + } + catch { /* Silently fail if we can't write logs */ } throw new FileNotFoundException("osqueryi.exe not found", path); } @@ -42,8 +53,12 @@ namespace LD_SysInfo.Services process.WaitForExit(); // Optional debug logging - File.AppendAllText("osquery_debug.log", - $"[{DateTime.Now}] Ran query: {sql}\nOutput length: {output.Length}\n"); + try + { + File.AppendAllText(GetLogPath("osquery_debug.log"), + $"[{DateTime.Now}] Ran query: {sql}\nOutput length: {output.Length}\n"); + } + catch { /* Silently fail if we can't write logs */ } return output; } @@ -69,8 +84,12 @@ namespace LD_SysInfo.Services } catch (Exception ex) { - File.AppendAllText("osquery_error.log", - $"[{DateTime.Now}] ⚠️ JSON parse failed for query '{sql}': {ex.Message}\n"); + try + { + File.AppendAllText(GetLogPath("osquery_error.log"), + $"[{DateTime.Now}] ⚠️ JSON parse failed for query '{sql}': {ex.Message}\n"); + } + catch { /* Silently fail if we can't write logs */ } return new List>(); } } diff --git a/LD-SysInfo/SystemInfo.cs b/LD-SysInfo/SystemInfo.cs index 16329ec..f5c185b 100644 --- a/LD-SysInfo/SystemInfo.cs +++ b/LD-SysInfo/SystemInfo.cs @@ -218,7 +218,17 @@ namespace LD_SysInfo } catch (Exception ex) { - File.AppendAllText("osquery_error.log", $"[{DateTime.Now}] {ex}\n"); + try + { + string logDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "PSG-Oversight"); + Directory.CreateDirectory(logDir); + string logPath = Path.Combine(logDir, "osquery_error.log"); + File.AppendAllText(logPath, $"[{DateTime.Now}] {ex}\n"); + } + catch + { + // Silently fail if we can't write logs + } } return info;