Made the wrapper async+no-async so it does not loose the caller context.
This commit is contained in:
@@ -34,7 +34,17 @@ public static class CrashlogHelper
|
||||
/// <param name="applicationName">The name of the application to write to the crashlog</param>
|
||||
/// <returns>The result of the method</returns>
|
||||
public static T WrapWithCrashLog<T>(Func<T> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Logs the exception to a file
|
||||
/// </summary>
|
||||
/// <param name="ex">The exception to log</param>
|
||||
/// <param name="logdir">The directory to write the crashlog to</param>
|
||||
/// <param name="applicationName">The name of the application to write to the crashlog</param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user