From ce11c08aba5a93e32c0b344145152fac3de4c3b0 Mon Sep 17 00:00:00 2001 From: Gadfly Date: Fri, 12 Jul 2024 11:09:46 +0800 Subject: [PATCH] fix: log UnobservedTaskException --- src/App.axaml.cs | 56 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/App.axaml.cs b/src/App.axaml.cs index c3c679b6..db3c12a0 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -45,6 +45,17 @@ namespace SourceGit [STAThread] public static void Main(string[] args) { + AppDomain.CurrentDomain.UnhandledException += (sender, e) => + { + LogException(e.ExceptionObject as Exception); + }; + + TaskScheduler.UnobservedTaskException += (sender, e) => + { + LogException(e.Exception); + e.SetObserved(); + }; + try { if (TryLaunchedAsRebaseTodoEditor(args, out int exitTodo)) @@ -56,25 +67,7 @@ namespace SourceGit } catch (Exception ex) { - var builder = new StringBuilder(); - builder.Append($"Crash::: {ex.GetType().FullName}: {ex.Message}\n\n"); - builder.Append("----------------------------\n"); - builder.Append($"Version: {Assembly.GetExecutingAssembly().GetName().Version}\n"); - builder.Append($"OS: {Environment.OSVersion.ToString()}\n"); - builder.Append($"Framework: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}\n"); - builder.Append($"Source: {ex.Source}\n"); - builder.Append($"---------------------------\n\n"); - builder.Append(ex.StackTrace); - while (ex.InnerException != null) - { - ex = ex.InnerException; - builder.Append($"\n\nInnerException::: {ex.GetType().FullName}: {ex.Message}\n"); - builder.Append(ex.StackTrace); - } - - var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); - var file = Path.Combine(Native.OS.DataDir, $"crash_{time}.log"); - File.WriteAllText(file, builder.ToString()); + LogException(ex); } } @@ -338,6 +331,31 @@ namespace SourceGit } } + private static void LogException(Exception ex) + { + if (ex == null) return; + + var builder = new StringBuilder(); + builder.Append($"Crash::: {ex.GetType().FullName}: {ex.Message}\n\n"); + builder.Append("----------------------------\n"); + builder.Append($"Version: {Assembly.GetExecutingAssembly().GetName().Version}\n"); + builder.Append($"OS: {Environment.OSVersion.ToString()}\n"); + builder.Append($"Framework: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}\n"); + builder.Append($"Source: {ex.Source}\n"); + builder.Append($"---------------------------\n\n"); + builder.Append(ex.StackTrace); + while (ex.InnerException != null) + { + ex = ex.InnerException; + builder.Append($"\n\nInnerException::: {ex.GetType().FullName}: {ex.Message}\n"); + builder.Append(ex.StackTrace); + } + + var time = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); + var file = Path.Combine(Native.OS.DataDir, $"crash_{time}.log"); + File.WriteAllText(file, builder.ToString()); + } + private static void ShowSelfUpdateResult(object data) { Dispatcher.UIThread.Post(() =>