From 992a0fd90cd9ca93647d052d55c05fdb110ac1ce Mon Sep 17 00:00:00 2001 From: Kenneth Skovhede Date: Mon, 25 Nov 2024 15:09:23 +0100 Subject: [PATCH] Made the wrapper async+no-async so it does not loose the caller context. --- Duplicati/Library/Crashlog/CrashlogHelper.cs | 84 ++++++++++++-------- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/Duplicati/Library/Crashlog/CrashlogHelper.cs b/Duplicati/Library/Crashlog/CrashlogHelper.cs index e2de33c4a..95d502f8c 100644 --- a/Duplicati/Library/Crashlog/CrashlogHelper.cs +++ b/Duplicati/Library/Crashlog/CrashlogHelper.cs @@ -34,7 +34,17 @@ public static class CrashlogHelper /// The name of the application to write to the crashlog /// The result of the method public static T WrapWithCrashLog(Func method, string? logdir = null, string? applicationName = null) - => WrapWithCrashLog(() => Task.Run(() => method()), logdir, applicationName).ConfigureAwait(false).GetAwaiter().GetResult(); + { + try + { + return method(); + } + catch (Exception ex) + { + LogCrashException(ex, logdir, applicationName); + throw; + } + } /// /// Wraps a method in a try-catch block and logs any exceptions to a file @@ -51,44 +61,54 @@ public static class CrashlogHelper return await method().ConfigureAwait(false); } catch (Exception ex) + { + LogCrashException(ex, logdir, applicationName); + throw; + } + } + + /// + /// Logs the exception to a file + /// + /// The exception to log + /// The directory to write the crashlog to + /// The name of the application to write to the crashlog + public static void LogCrashException(Exception ex, string? logdir, string? applicationName) + { + try + { + Console.WriteLine("Crash! {0}{1}", Environment.NewLine, ex); + } + catch + { + } + + try + { + if (string.IsNullOrWhiteSpace(logdir)) + { + var def = DefaultLogDir; + logdir = string.IsNullOrWhiteSpace(def) + ? SystemTempPath + : def; + } + + applicationName = string.IsNullOrWhiteSpace(applicationName) + ? System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name ?? Guid.NewGuid().ToString()[..8] + : applicationName; + + var report_file = System.IO.Path.Combine(logdir, $"{applicationName}-crashlog.txt"); + System.IO.File.WriteAllText(report_file, ex.ToString()); + } + catch (Exception writeex) { try { - Console.WriteLine("Crash! {0}{1}", Environment.NewLine, ex); + Console.WriteLine("Failed to write crashlog: {0}", writeex); } catch { } - - try - { - if (string.IsNullOrWhiteSpace(logdir)) - { - var def = DefaultLogDir; - logdir = string.IsNullOrWhiteSpace(def) - ? SystemTempPath - : def; - } - - applicationName = string.IsNullOrWhiteSpace(applicationName) - ? System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name ?? Guid.NewGuid().ToString()[..8] - : applicationName; - - var report_file = System.IO.Path.Combine(logdir, $"{applicationName}-crashlog.txt"); - System.IO.File.WriteAllText(report_file, ex.ToString()); - } - catch (Exception writeex) - { - try - { - Console.WriteLine("Failed to write crashlog: {0}", writeex); - } - catch - { - } - } - - throw; } } }