diff --git a/OpenSharpCAD/MainWindow.cs b/OpenSharpCAD/MainWindow.cs index b41d2ce..37caa0b 100644 --- a/OpenSharpCAD/MainWindow.cs +++ b/OpenSharpCAD/MainWindow.cs @@ -213,13 +213,20 @@ private void Compile() } private void LogException(Exception ex) { + // Always output to console so the user can see what happened + Console.Error.WriteLine($"ERROR: {ex.Message}"); + Console.Error.WriteLine(ex.StackTrace); + try { - string logDir = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - "CSharpCAD", - "logs" - ); + string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + if (string.IsNullOrEmpty(baseDir)) + { + baseDir = Path.GetTempPath(); + } + + string logDir = Path.Combine(baseDir, "CSharpCAD", "logs"); + if (!Directory.Exists(logDir)) { Directory.CreateDirectory(logDir); diff --git a/OpenSharpCAD/Program.cs b/OpenSharpCAD/Program.cs index 74b4584..a76a15f 100644 --- a/OpenSharpCAD/Program.cs +++ b/OpenSharpCAD/Program.cs @@ -37,19 +37,32 @@ static void Main(string[] args) } catch (Exception ex) { - string logDir = Path.Combine( - Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), - "CSharpCAD", - "logs" - ); + // Always output to console so the user can see what happened + Console.Error.WriteLine($"GLOBAL ERROR: {ex.Message}"); + Console.Error.WriteLine(ex.StackTrace); - if (!Directory.Exists(logDir)) + try { - Directory.CreateDirectory(logDir); + string baseDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + if (string.IsNullOrEmpty(baseDir)) + { + baseDir = Path.GetTempPath(); + } + + string logDir = Path.Combine(baseDir, "CSharpCAD", "logs"); + + if (!Directory.Exists(logDir)) + { + Directory.CreateDirectory(logDir); + } + string logPath = Path.Combine(logDir, "errors.log"); + string logMessage = $"[{DateTime.Now}] GLOBAL ERROR: {ex.Message}{Environment.NewLine}{ex.StackTrace}{Environment.NewLine}{new string('=', 30)}{Environment.NewLine}"; + File.AppendAllText(logPath, logMessage); + } + catch (Exception logEx) + { + Console.Error.WriteLine($"Failed to write to log file: {logEx.Message}"); } - string logPath = Path.Combine(logDir, "errors.log"); - string logMessage = $"[{DateTime.Now}] GLOBAL ERROR: {ex.Message}{Environment.NewLine}{ex.StackTrace}{Environment.NewLine}{new string('=', 30)}{Environment.NewLine}"; - File.AppendAllText(logPath, logMessage); } } }