From 68f215614e53e6f356164bcac72504f01a650e0d Mon Sep 17 00:00:00 2001 From: Henry Lin Date: Tue, 6 Oct 2020 14:58:09 +0800 Subject: [PATCH] Added exception handling, fixed missing DLLs --- QuickDictionary/App.xaml | 3 +- QuickDictionary/App.xaml.cs | 40 ++++++++++++++++++++++ QuickDictionary/MainWindow.xaml.cs | 2 +- QuickDictionary/Properties/AssemblyInfo.cs | 4 +-- QuickDictionary/QuickDictionary.csproj | 27 +++++++++++++++ QuickDictionary/QuickDictionary.nuspec | 2 +- 6 files changed, 73 insertions(+), 5 deletions(-) diff --git a/QuickDictionary/App.xaml b/QuickDictionary/App.xaml index b3e3f99..6ca8d41 100644 --- a/QuickDictionary/App.xaml +++ b/QuickDictionary/App.xaml @@ -2,7 +2,8 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:QuickDictionary" - StartupUri="MainWindow.xaml"> + StartupUri="MainWindow.xaml" + Startup="Application_Startup"> diff --git a/QuickDictionary/App.xaml.cs b/QuickDictionary/App.xaml.cs index a504b46..eca04da 100644 --- a/QuickDictionary/App.xaml.cs +++ b/QuickDictionary/App.xaml.cs @@ -6,6 +6,7 @@ using System.Data; using System.IO; using System.Linq; +using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; @@ -43,6 +44,45 @@ private void Application_Startup(object sender, StartupEventArgs e) { Current.Shutdown(); } + + SetupExceptionHandling(); + } + + private void SetupExceptionHandling() + { + AppDomain.CurrentDomain.UnhandledException += (s, e) => + LogUnhandledException((Exception)e.ExceptionObject, "AppDomain.CurrentDomain.UnhandledException"); + + DispatcherUnhandledException += (s, e) => + { + LogUnhandledException(e.Exception, "Application.Current.DispatcherUnhandledException"); + e.Handled = true; + }; + + TaskScheduler.UnobservedTaskException += (s, e) => + { + LogUnhandledException(e.Exception, "TaskScheduler.UnobservedTaskException"); + e.SetObserved(); + }; + } + + private void LogUnhandledException(Exception exception, string source) + { + StringBuilder message = new StringBuilder(); + message.AppendLine($"Unhandled exception ({source})"); + try + { + System.Reflection.AssemblyName assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName(); + message.AppendLine($" in {assemblyName.Name} v{assemblyName.Version}"); + } + catch (Exception ex) + { + } + message.AppendLine(exception.Message); + message.AppendLine(exception.StackTrace); + message.AppendLine(); + message.AppendLine(); + File.AppendAllText("log.txt", message.ToString()); } } } diff --git a/QuickDictionary/MainWindow.xaml.cs b/QuickDictionary/MainWindow.xaml.cs index c95d8c4..e8f2e14 100644 --- a/QuickDictionary/MainWindow.xaml.cs +++ b/QuickDictionary/MainWindow.xaml.cs @@ -195,7 +195,7 @@ private async void Window_SourceInitialized(object sender, EventArgs e) using (var mgr = await UpdateManager.GitHubUpdateManager("https://github.com/Henry-YSLin/QuickDictionary")) { - await mgr.UpdateApp((progress) => Dispatcher.Invoke(() => Title = title + $" - Updating {progress}%")); + await mgr.UpdateApp((progress) => Dispatcher.Invoke(() => Title = title + (progress > 95 ? "" : $" - Updating {progress}%"))); } } diff --git a/QuickDictionary/Properties/AssemblyInfo.cs b/QuickDictionary/Properties/AssemblyInfo.cs index e846459..5a1a837 100644 --- a/QuickDictionary/Properties/AssemblyInfo.cs +++ b/QuickDictionary/Properties/AssemblyInfo.cs @@ -51,5 +51,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.0.1.0")] +[assembly: AssemblyFileVersion("1.0.1.0")] diff --git a/QuickDictionary/QuickDictionary.csproj b/QuickDictionary/QuickDictionary.csproj index 2cc8e7b..1b00362 100644 --- a/QuickDictionary/QuickDictionary.csproj +++ b/QuickDictionary/QuickDictionary.csproj @@ -21,6 +21,21 @@ true + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.1.0 + false + true AnyCPU @@ -217,6 +232,18 @@ + + + False + Microsoft .NET Framework 4.7.2 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 + false + + diff --git a/QuickDictionary/QuickDictionary.nuspec b/QuickDictionary/QuickDictionary.nuspec index 2a3179e..34b3847 100644 --- a/QuickDictionary/QuickDictionary.nuspec +++ b/QuickDictionary/QuickDictionary.nuspec @@ -12,6 +12,6 @@ - + \ No newline at end of file