From c5b193376349e0b514995b33c5ec274d925629e1 Mon Sep 17 00:00:00 2001 From: Cherry <13651622+MolotovCherry@users.noreply.github.com> Date: Mon, 20 Nov 2023 13:12:23 -0800 Subject: [PATCH] Add logging functionality (especially for exceptions) --- Virtual Display Driver Control/App.xaml.cs | 59 +++++++++++++++++++ .../Common/Logging.cs | 25 ++++++++ .../Virtual Display Driver Control.csproj | 3 + 3 files changed, 87 insertions(+) create mode 100644 Virtual Display Driver Control/Common/Logging.cs diff --git a/Virtual Display Driver Control/App.xaml.cs b/Virtual Display Driver Control/App.xaml.cs index 9eea689..ce06952 100644 --- a/Virtual Display Driver Control/App.xaml.cs +++ b/Virtual Display Driver Control/App.xaml.cs @@ -1,4 +1,8 @@ using Microsoft.UI.Xaml; +using Serilog; +using System; +using System.Diagnostics; +using System.Text; using Virtual_Display_Driver_Control.Common; using Virtual_Display_Driver_Control.Helpers; @@ -11,6 +15,10 @@ public partial class App : Application { #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. public App() { + Logging.Initialize(); + + UnhandledException += App_UnhandledException; + AppDomain.CurrentDomain.UnhandledException += Domain_UnhandledException; InitializeComponent(); Settings = SettingsProvider.Initialize(); } @@ -22,5 +30,56 @@ public partial class App : Application { MaterialHelper.Initialize(); Window.Activate(); + Window.Closed += OnClosed; + } + + private void OnClosed(object sender, WindowEventArgs e) { + // cleanup ops + Log.CloseAndFlush(); + } + + void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) { + _UnhandledException(sender, e.Exception); + } + + void Domain_UnhandledException(object sender, System.UnhandledExceptionEventArgs e) { + _UnhandledException(sender, (Exception)e.ExceptionObject); + } + + async void _UnhandledException(object sender, Exception ex) { + StringBuilder formattedException = new StringBuilder() { Capacity = 200 }; + + formattedException.Append("\n--------- UNHANDLED EXCEPTION ---------"); + + if (ex is not null) { + formattedException.Append($"\n>>>> HRESULT: {ex.HResult}\n"); + if (ex.Message is not null) { + formattedException.Append("\n--- MESSAGE ---\n"); + formattedException.Append(ex.Message); + } + if (ex.StackTrace is not null) { + formattedException.Append("\n--- STACKTRACE ---\n"); + formattedException.Append(ex.StackTrace); + } + if (ex.Source is not null) { + formattedException.Append("\n--- SOURCE ---\n"); + formattedException.Append(ex.Source); + } + if (ex.InnerException is not null) { + formattedException.Append("\n--- INNER ---\n"); + formattedException.Append(ex.InnerException); + } + } else { + formattedException.Append("\nException is null!\n"); + } + + formattedException.Append("\n---------------------------------------\n"); + + Log.Fatal(formattedException.ToString()); + + Log.CloseAndFlush(); + + // Please check "Output Window" for exception details (View -> Output Window) (CTRL + ALT + O) + Debugger.Break(); } } diff --git a/Virtual Display Driver Control/Common/Logging.cs b/Virtual Display Driver Control/Common/Logging.cs new file mode 100644 index 0000000..f27af44 --- /dev/null +++ b/Virtual Display Driver Control/Common/Logging.cs @@ -0,0 +1,25 @@ +using Serilog; +using System; +using System.IO; + +namespace Virtual_Display_Driver_Control.Common; +class Logging { + public static void Initialize() { + var log = new LoggerConfiguration() + // Always log to debug regardless + .WriteTo.Debug(); + + // Write output log if not in debug mode + #if !DEBUG + log.WriteTo.File(Path.Combine(SettingsProvider.AppDir, "app.log"), + rollingInterval: RollingInterval.Day, + rollOnFileSizeLimit: true); + #endif + + Log.Logger = log.CreateLogger(); + } + + public static void Dispose() { + Log.CloseAndFlush(); + } +} diff --git a/Virtual Display Driver Control/Virtual Display Driver Control.csproj b/Virtual Display Driver Control/Virtual Display Driver Control.csproj index c1019d9..3dd12af 100644 --- a/Virtual Display Driver Control/Virtual Display Driver Control.csproj +++ b/Virtual Display Driver Control/Virtual Display Driver Control.csproj @@ -50,6 +50,9 @@ + + + all