From 93053111c1be3086e8f7c0b684664a44741390b7 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Wed, 3 Jul 2024 17:01:15 +0200 Subject: [PATCH] Set error.type attribute (#42) * Set error.type attribute * Rename attribute name --- Backtrace/Base/BacktraceBase.cs | 21 ++++++++++++++------- Backtrace/Model/BacktraceReport.cs | 15 +++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Backtrace/Base/BacktraceBase.cs b/Backtrace/Base/BacktraceBase.cs index d2eb665..877aa4b 100644 --- a/Backtrace/Base/BacktraceBase.cs +++ b/Backtrace/Base/BacktraceBase.cs @@ -319,8 +319,7 @@ public virtual void HandleApplicationException() /// public virtual void HandleApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { - var exception = e.Exception as Exception; - Send(new BacktraceReport(exception)); + ReportUnhandledException(e.Exception); OnUnhandledApplicationException?.Invoke(e.Exception); } @@ -346,18 +345,26 @@ private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionE private void ReportUnhandledException(Exception exception) { + if (UnpackAggregateExcetpion && exception is AggregateException) + { + exception = exception.InnerException; + } // Always prefer to use Database rather than freezing the app because of the http request. // On environment where the database cannot be enabled for any reason, we still want to report // data and in this situation we prefer to take this tradeoff. var report = new BacktraceReport(exception); + report.SetReportErrorType("Unhandled Exception"); if (Database != null) { - Database.Add(report, Attributes, MiniDumpType); - } - else - { - Send(report); + var result = Database.Add(report, Attributes, MiniDumpType); + // try to send anyway if the database refuses to accept a report for any reason. + if (result != null) + { + return; + } } + Send(report); + } #endif } diff --git a/Backtrace/Model/BacktraceReport.cs b/Backtrace/Model/BacktraceReport.cs index 35c00cf..f40b2c1 100644 --- a/Backtrace/Model/BacktraceReport.cs +++ b/Backtrace/Model/BacktraceReport.cs @@ -93,6 +93,8 @@ public class BacktraceReport internal readonly bool _reflectionMethodName; + private const string ERROR_TYPE_ATTRIBUTE = "error.type"; + /// /// Create new instance of Backtrace report to sending a report with custom client message /// @@ -108,6 +110,10 @@ public BacktraceReport( : this(null as Exception, attributes, attachmentPaths, reflectionMethodName) { Message = message; + if (!Attributes.ContainsKey(ERROR_TYPE_ATTRIBUTE)) + { + SetReportErrorType("Message"); + } } /// @@ -131,6 +137,10 @@ public BacktraceReport( _reflectionMethodName = reflectionMethodName; Message = ExceptionTypeReport ? exception.Message : string.Empty; SetCallingAssemblyInformation(); + if (ExceptionTypeReport && !Attributes.ContainsKey(ERROR_TYPE_ATTRIBUTE)) + { + SetReportErrorType("Exception"); + } } /// @@ -193,5 +203,10 @@ internal BacktraceReport CreateInnerReport() copy.Classifier = copy.Exception.GetType().Name; return copy; } + + internal void SetReportErrorType(string errorType) + { + Attributes[ERROR_TYPE_ATTRIBUTE] = errorType; + } } }