From 1fe2023b51ce75de8fae9a7c8a7bcd4e776d78cb Mon Sep 17 00:00:00 2001 From: Anton Kasyanov Date: Sat, 17 Jun 2017 13:29:09 +0300 Subject: [PATCH] Exception information logging --- .../ApplicationBase/ExceptionHandler.cs | 50 +++++++++++++++++++ Eve-O-Preview/Eve-O-Preview.csproj | 1 + Eve-O-Preview/Program.cs | 6 +-- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 Eve-O-Preview/ApplicationBase/ExceptionHandler.cs diff --git a/Eve-O-Preview/ApplicationBase/ExceptionHandler.cs b/Eve-O-Preview/ApplicationBase/ExceptionHandler.cs new file mode 100644 index 0000000..6489202 --- /dev/null +++ b/Eve-O-Preview/ApplicationBase/ExceptionHandler.cs @@ -0,0 +1,50 @@ +using System; +using System.IO; +using System.Threading; +using System.Windows.Forms; + +namespace EveOPreview +{ + // A really very primitive exception handler stuff here + // No IoC, no fancy DI containers - just a plain exception stacktrace dump + // If this code is called then something was gone really bad + // so even the DI infrastructure might be dead already. + // So this dumb and non elegant approach is used + sealed class ExceptionHandler + { + private const string ExceptionDumpFileName = "EVE-O Preview.log"; + private const string ExceptionMessage = "EVE-O Preview has encountered a problem and needs to close. Additional information has been saved in the crash log file."; + + public void SetupExceptionHandlers() + { + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + Application.ThreadException += delegate (Object sender, ThreadExceptionEventArgs e) + { + this.ExceptionEventHandler(e.Exception); + }; + + AppDomain.CurrentDomain.UnhandledException += delegate (Object sender, UnhandledExceptionEventArgs e) + { + this.ExceptionEventHandler(e.ExceptionObject as Exception); + }; + } + + private void ExceptionEventHandler(Exception exception) + { + try + { + String exceptionMessage = exception.ToString(); + File.WriteAllText(ExceptionHandler.ExceptionDumpFileName, exceptionMessage); + + MessageBox.Show(ExceptionHandler.ExceptionMessage, @"EVE-O Preview", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + catch + { + // We are in unstable state now so even this operation might fail + // Still we actually don't care anymore - anyway the application has been cashed + } + + System.Environment.Exit(1); + } + } +} \ No newline at end of file diff --git a/Eve-O-Preview/Eve-O-Preview.csproj b/Eve-O-Preview/Eve-O-Preview.csproj index 72b015f..b27ec26 100644 --- a/Eve-O-Preview/Eve-O-Preview.csproj +++ b/Eve-O-Preview/Eve-O-Preview.csproj @@ -88,6 +88,7 @@ + diff --git a/Eve-O-Preview/Program.cs b/Eve-O-Preview/Program.cs index c25e9c4..494c8de 100644 --- a/Eve-O-Preview/Program.cs +++ b/Eve-O-Preview/Program.cs @@ -27,15 +27,15 @@ namespace EveOPreview return; } + ExceptionHandler handler = new ExceptionHandler(); + handler.SetupExceptionHandlers(); + Program.InitializeWinFormsGui(); IApplicationController controller = Program.InitializeApplicationController(); - Program.SetupApplicationConttroller(controller, Program.GetCustomConfigFile(args)); controller.Run(); - - token = null; } private static string GetCustomConfigFile(string[] args)