Skip to content

Commit

Permalink
Fixed a bug where Application.ThreadException, HttpApplication.Error,…
Browse files Browse the repository at this point in the history
… Application.Current.DispatcherUnhandledException may not be logged (#308)
  • Loading branch information
niemyjski authored May 18, 2023
1 parent 61e3291 commit fda1479
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/Exceptionless/Extensions/ExceptionlessClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using Exceptionless.Logging;
using Exceptionless.Models;

#pragma warning disable AsyncFixer03

namespace Exceptionless {
public static class ExceptionlessClientExtensions {
/// <summary>
Expand Down Expand Up @@ -385,9 +383,10 @@ public static void RegisterAppDomainUnhandledExceptionHandler(this Exceptionless
}

log.Info(typeof(ExceptionlessClient), "AppDomain.CurrentDomain.UnhandledException finished");
log.Flush();
} catch (Exception ex) {
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing AppDomain unhandled exception: ", ex.Message));
} finally {
log.Flush();
}
};
}
Expand Down Expand Up @@ -430,9 +429,10 @@ public static void RegisterOnProcessExitHandler(this ExceptionlessClient client)
}

log.Info(typeof(ExceptionlessClient), "ProcessExit finished");
log.Flush();
} catch (Exception ex) {
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing process exit: ", ex.Message));
} finally {
log.Flush();
}
};
}
Expand Down
26 changes: 20 additions & 6 deletions src/Platforms/Exceptionless.Web/ExceptionlessClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@ public static void RegisterHttpApplicationErrorHandler(this ExceptionlessClient
if (HttpContext.Current == null)
return;

Exception exception = HttpContext.Current.Server.GetLastError();
var exception = HttpContext.Current.Server.GetLastError();
if (exception == null)
return;

var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("HttpApplicationError");
contextData.Add("HttpContext", HttpContext.Current.ToWrapped());
var log = client.Configuration.Resolver.GetLog();
try {
log.Info(typeof(ExceptionlessClient), "HttpApplication.Error called");

exception.ToExceptionless(contextData, client).Submit();
var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("HttpApplicationError");
contextData.Add("HttpContext", HttpContext.Current.ToWrapped());

exception.ToExceptionless(contextData, client).Submit();

// process queue immediately since the app is about to exit.
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();

log.Info(typeof(ExceptionlessClient), "HttpApplication.Error finished");
} catch (Exception ex) {
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing HttpApplication unhandled exception: ", ex.Message));
} finally {
log.Flush();
}
};

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ public static void RegisterApplicationThreadExceptionHandler(this ExceptionlessC
if (client == null)
throw new ArgumentNullException(nameof(client));

if (_onApplicationThreadException == null)
if (_onApplicationThreadException == null) {
_onApplicationThreadException = (sender, args) => {
var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("ApplicationThreadException");

args.Exception.ToExceptionless(contextData, client).Submit();
var log = client.Configuration.Resolver.GetLog();
try {
log.Info(typeof(ExceptionlessClient), "Application.ThreadException called");
var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("ApplicationThreadException");

args.Exception.ToExceptionless(contextData, client).Submit();

// process queue immediately since the app is about to exit.
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();

log.Info(typeof(ExceptionlessClient), "Application.ThreadException finished");
} catch (Exception ex) {
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing Application Thread Exception: ", ex.Message));
} finally {
log.Flush();
}
};
}

try {
Application.ThreadException -= _onApplicationThreadException;
Expand Down
28 changes: 17 additions & 11 deletions src/Platforms/Exceptionless.Wpf/ExceptionlessClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using Exceptionless.Plugins;
using Exceptionless.Logging;

#pragma warning disable AsyncFixer03

namespace Exceptionless.Wpf.Extensions {
public static class ExceptionlessClientExtensions {
private static DispatcherUnhandledExceptionEventHandler _onApplicationDispatcherUnhandledException;
Expand All @@ -17,22 +15,30 @@ public static void RegisterApplicationDispatcherUnhandledExceptionHandler(this E
if (System.Windows.Application.Current == null)
return;

if (_onApplicationDispatcherUnhandledException == null)
_onApplicationDispatcherUnhandledException = async (sender, args) => {
var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("DispatcherUnhandledException");

args.Exception.ToExceptionless(contextData, client).Submit();
if (_onApplicationDispatcherUnhandledException == null) {
_onApplicationDispatcherUnhandledException = (sender, args) => {
var log = client.Configuration.Resolver.GetLog();

try {
log.Info(typeof(ExceptionlessClient), "Application.Current.DispatcherUnhandledException called");

var contextData = new ContextData();
contextData.MarkAsUnhandledError();
contextData.SetSubmissionMethod("DispatcherUnhandledException");

args.Exception.ToExceptionless(contextData, client).Submit();

// process queue immediately since the app is about to exit.
await client.ProcessQueueAsync().ConfigureAwait(false);
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();

log.Info(typeof(ExceptionlessClient), "Application.Current.DispatcherUnhandledException finished");
} catch (Exception ex) {
var log = client.Configuration.Resolver.GetLog();
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing application dispatcher exception: ", ex.Message));
} finally {
log.Flush();
}
};
}

try {
System.Windows.Application.Current.DispatcherUnhandledException -= _onApplicationDispatcherUnhandledException;
Expand Down

0 comments on commit fda1479

Please sign in to comment.