From 5043060ee2d7cb143238c354d9aa5f72f44164c1 Mon Sep 17 00:00:00 2001 From: Xjph Date: Tue, 22 Feb 2022 13:30:01 -0330 Subject: [PATCH] Add top level error logging. (#55) * Add top level error logging. * Remove test exception, chain stringbuilders. --- ObservatoryCore/ObservatoryCore.cs | 37 ++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/ObservatoryCore/ObservatoryCore.cs b/ObservatoryCore/ObservatoryCore.cs index 2eb65ec..d7ba356 100644 --- a/ObservatoryCore/ObservatoryCore.cs +++ b/ObservatoryCore/ObservatoryCore.cs @@ -10,13 +10,40 @@ namespace Observatory static void Main(string[] args) { string version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString(); - if (Properties.Core.Default.CoreVersion != version) + try { - Properties.Core.Default.Upgrade(); - Properties.Core.Default.CoreVersion = version; - Properties.Core.Default.Save(); + if (Properties.Core.Default.CoreVersion != version) + { + Properties.Core.Default.Upgrade(); + Properties.Core.Default.CoreVersion = version; + Properties.Core.Default.Save(); + } + BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); } - BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); + catch (Exception ex) + { + var docPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); + var errorMessage = new System.Text.StringBuilder(); + errorMessage + .AppendLine($"Error encountered in Elite Observatory {version}.") + .AppendLine(FormatExceptionMessage(ex)) + .AppendLine(); + System.IO.File.AppendAllText(docPath + System.IO.Path.DirectorySeparatorChar + "ObservatoryErrorLog.txt", errorMessage.ToString()); + } + + } + + static string FormatExceptionMessage(Exception ex, bool inner = false) + { + var errorMessage = new System.Text.StringBuilder(); + errorMessage + .AppendLine($"{(inner ? "Inner e" : "E")}xception message: {ex.Message}") + .AppendLine($"Stack trace:") + .AppendLine(ex.StackTrace); + if (ex.InnerException != null) + errorMessage.AppendLine(FormatExceptionMessage(ex.InnerException, true)); + + return errorMessage.ToString(); } public static AppBuilder BuildAvaloniaApp()