diff --git a/src/JASM.AutoUpdater/App.xaml.cs b/src/JASM.AutoUpdater/App.xaml.cs index 8eb67872..fbb9dab3 100644 --- a/src/JASM.AutoUpdater/App.xaml.cs +++ b/src/JASM.AutoUpdater/App.xaml.cs @@ -1,5 +1,10 @@ using System; +using System.IO; using System.Linq; +using System.Reflection; +using Windows.Win32; +using Windows.Win32.Foundation; +using Windows.Win32.UI.WindowsAndMessaging; using Microsoft.UI.Xaml; using Serilog; using WinUIEx; @@ -24,16 +29,21 @@ public App() InitializeComponent(); - var logger = new LoggerConfiguration() - .Enrich.FromLogContext() - .MinimumLevel.Information() - .WriteTo.File("logs\\auto-updater-log.txt") - .WriteTo.Console() - .CreateLogger(); + var logger = CreateLogger(); Log.Logger = logger; } + private static ILogger CreateLogger() => new LoggerConfiguration() + .Enrich.FromLogContext() + .MinimumLevel.Information() + .WriteTo.File(LogFilePath) + .WriteTo.Console() + .CreateLogger(); + + private static readonly string LogFilePath = + $@"{Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)}\logs\auto-updater-log.txt"; + /// /// Invoked when the application is launched. /// @@ -61,7 +71,42 @@ protected override void OnLaunched(LaunchActivatedEventArgs args) private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { Log.Error(e.Exception, "Unhandled exception"); + Log.Information("------"); + Log.CloseAndFlush(); + + e.Handled = true; + + var result = PInvoke.MessageBox( + new HWND(WinRT.Interop.WindowNative.GetWindowHandle(MainWindow)), + $"Check the logs file for more info: {LogFilePath}\n\n" + + $"Press Yes to close the program. Press No to ignore the error and continue", + $"An error occured: {e.Exception.GetType()} | {e.Exception.Message ?? "null"}", + MESSAGEBOX_STYLE.MB_ICONSTOP | MESSAGEBOX_STYLE.MB_YESNO | MESSAGEBOX_STYLE.MB_APPLMODAL | + MESSAGEBOX_STYLE.MB_SETFOREGROUND); + + if (result == MESSAGEBOX_RESULT.IDYES) + { + Current.Exit(); + return; + } + + Log.Logger = CreateLogger(); + + var mainPage = MainWindow.Content as MainPage; + MainWindow.DispatcherQueue.TryEnqueue(() => + { + try + { + mainPage?.ViewModel?.Stop("An unknown error occured.."); + } + catch (Exception exception) + { + return; + } + }); + + return; } - internal static MainWindow MainWindow; + internal static MainWindow MainWindow = null!; } \ No newline at end of file diff --git a/src/JASM.AutoUpdater/JASM.AutoUpdater.csproj b/src/JASM.AutoUpdater/JASM.AutoUpdater.csproj index e1347525..2fe8f46d 100644 --- a/src/JASM.AutoUpdater/JASM.AutoUpdater.csproj +++ b/src/JASM.AutoUpdater/JASM.AutoUpdater.csproj @@ -20,11 +20,19 @@ + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/src/JASM.AutoUpdater/MainPageVM.cs b/src/JASM.AutoUpdater/MainPageVM.cs index 3fa58ea3..b83aaa23 100644 --- a/src/JASM.AutoUpdater/MainPageVM.cs +++ b/src/JASM.AutoUpdater/MainPageVM.cs @@ -178,7 +178,7 @@ private void Finish() return release; } - private void Stop(string stopReason) + public void Stop(string stopReason) { Stopped = true; StopReason = stopReason; diff --git a/src/JASM.AutoUpdater/NativeMethods.txt b/src/JASM.AutoUpdater/NativeMethods.txt new file mode 100644 index 00000000..cb0259b4 --- /dev/null +++ b/src/JASM.AutoUpdater/NativeMethods.txt @@ -0,0 +1 @@ +MessageBox \ No newline at end of file