From 06b1163a5210aa920056ceb1cb6ad2461c53399e Mon Sep 17 00:00:00 2001 From: goat <16760685+goaaats@users.noreply.github.com> Date: Sat, 21 Aug 2021 14:31:01 +0200 Subject: [PATCH] feat: serialize last exception for franzbot --- Dalamud/EntryPoint.cs | 1 + Dalamud/Troubleshooting.cs | 40 +++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Dalamud/EntryPoint.cs b/Dalamud/EntryPoint.cs index cac591a99..befc59c9b 100644 --- a/Dalamud/EntryPoint.cs +++ b/Dalamud/EntryPoint.cs @@ -196,6 +196,7 @@ namespace Dalamud { case Exception ex: Log.Fatal(ex, "Unhandled exception on AppDomain"); + Troubleshooting.LogException(ex, "DalamudUnhandled"); var info = "Further information could not be obtained"; if (ex.TargetSite != null && ex.TargetSite.DeclaringType != null) diff --git a/Dalamud/Troubleshooting.cs b/Dalamud/Troubleshooting.cs index 7aba6ec2c..c2cfa2e7e 100644 --- a/Dalamud/Troubleshooting.cs +++ b/Dalamud/Troubleshooting.cs @@ -14,14 +14,39 @@ namespace Dalamud /// /// Class responsible for printing troubleshooting information to the log. /// - internal static class Troubleshooting + public static class Troubleshooting { /// - /// Log troubleshooting information to Serilog. + /// Log the last exception in a parseable format to serilog. + /// + /// The exception to log. + /// Additional context. + public static void LogException(Exception exception, string context) + { + try + { + var payload = new ExceptionPayload + { + Context = context, + When = DateTime.Now, + Info = exception.ToString(), + }; + + var encodedPayload = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(payload))); + Log.Information($"LASTEXCEPTION:{encodedPayload}"); + } + catch (Exception ex) + { + Log.Error(ex, "Could not print exception."); + } + } + + /// + /// Log troubleshooting information in a parseable format to Serilog. /// /// The instance to read information from. /// Whether or not the interface was loaded. - public static void LogTroubleshooting(Dalamud dalamud, bool isInterfaceLoaded) + internal static void LogTroubleshooting(Dalamud dalamud, bool isInterfaceLoaded) { try { @@ -47,6 +72,15 @@ namespace Dalamud } } + private class ExceptionPayload + { + public DateTime When { get; set; } + + public string Info { get; set; } + + public string Context { get; set; } + } + private class TroubleshootingPayload { public PluginManifest[] LoadedPlugins { get; set; }