Fix: Use AppData directory for log files to avoid permission errors
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<Dictionary<string, string>>();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user