From a676537e26c199d1f238fa64dc7fd25bdcf97470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Sto=C5=A1i=C4=87?= Date: Thu, 24 Sep 2020 12:29:53 +0200 Subject: [PATCH] [FancyZones editor] Rudimentary crash handler (#6783) * Handle crashes in the FZ editor * Removed reference to .NET frameworks, added a message box * log => txt * Update text shown --- .../editor/FancyZonesEditor/App.xaml.cs | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs index d2d49bbddc..245e8c67dd 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs @@ -3,7 +3,12 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; using System.Runtime.InteropServices; +using System.Text; using System.Threading.Tasks; using System.Windows; using FancyZonesEditor.Models; @@ -25,6 +30,8 @@ namespace FancyZonesEditor private void OnStartup(object sender, StartupEventArgs e) { + AppDomain.CurrentDomain.UnhandledException += OnUnhandledException; + RunnerHelper.WaitForPowerToysRunner(Settings.PowerToysPID, () => { Environment.Exit(0); @@ -66,5 +73,94 @@ namespace FancyZonesEditor overlay.Show(); overlay.DataContext = foundModel; } + + private void OnUnhandledException(object sender, UnhandledExceptionEventArgs args) + { + var fileStream = File.OpenWrite("FZEditorCrashLog.txt"); + var sw = new StreamWriter(fileStream); + sw.Write(FormatException((Exception)args.ExceptionObject)); + fileStream.Close(); + MessageBox.Show("Error logged to " + Path.GetFullPath(fileStream.Name) + "\nPlease report the bug to https://aka.ms/powerToysReportBug", "FancyZones Editor Error"); + } + + private string FormatException(Exception ex) + { + var sb = new StringBuilder(); + sb.AppendLine(); + sb.AppendLine("## Exception"); + sb.AppendLine(); + sb.AppendLine("```"); + + var exlist = new List(); + + while (ex != null) + { + var exsb = new StringBuilder(); + exsb.Append(ex.GetType().FullName); + exsb.Append(": "); + exsb.AppendLine(ex.Message); + if (ex.Source != null) + { + exsb.Append(" Source: "); + exsb.AppendLine(ex.Source); + } + + if (ex.TargetSite != null) + { + exsb.Append(" TargetAssembly: "); + exsb.AppendLine(ex.TargetSite.Module.Assembly.ToString()); + exsb.Append(" TargetModule: "); + exsb.AppendLine(ex.TargetSite.Module.ToString()); + exsb.Append(" TargetSite: "); + exsb.AppendLine(ex.TargetSite.ToString()); + } + + exsb.AppendLine(ex.StackTrace); + exlist.Add(exsb); + + ex = ex.InnerException; + } + + foreach (var result in exlist.Select(o => o.ToString()).Reverse()) + { + sb.AppendLine(result); + } + + sb.AppendLine("```"); + sb.AppendLine(); + + sb.AppendLine("## Environment"); + sb.AppendLine($"* Command Line: {Environment.CommandLine}"); + sb.AppendLine($"* Timestamp: {DateTime.Now.ToString(CultureInfo.InvariantCulture)}"); + sb.AppendLine($"* OS Version: {Environment.OSVersion.VersionString}"); + sb.AppendLine($"* IntPtr Length: {IntPtr.Size}"); + sb.AppendLine($"* x64: {Environment.Is64BitOperatingSystem}"); + sb.AppendLine($"* CLR Version: {Environment.Version}"); + sb.AppendLine("## Assemblies - " + AppDomain.CurrentDomain.FriendlyName); + sb.AppendLine(); + foreach (var ass in AppDomain.CurrentDomain.GetAssemblies().OrderBy(o => o.GlobalAssemblyCache ? 50 : 0)) + { + sb.Append("* "); + sb.Append(ass.FullName); + sb.Append(" ("); + + if (ass.IsDynamic) + { + sb.Append("dynamic assembly doesn't has location"); + } + else if (string.IsNullOrEmpty(ass.Location)) + { + sb.Append("location is null or empty"); + } + else + { + sb.Append(ass.Location); + } + + sb.AppendLine(")"); + } + + return sb.ToString(); + } } }