Skip to content

Commit fda1479

Browse files
authored
Fixed a bug where Application.ThreadException, HttpApplication.Error, Application.Current.DispatcherUnhandledException may not be logged (#308)
1 parent 61e3291 commit fda1479

File tree

4 files changed

+61
-27
lines changed

4 files changed

+61
-27
lines changed

src/Exceptionless/Extensions/ExceptionlessClientExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using Exceptionless.Logging;
77
using Exceptionless.Models;
88

9-
#pragma warning disable AsyncFixer03
10-
119
namespace Exceptionless {
1210
public static class ExceptionlessClientExtensions {
1311
/// <summary>
@@ -385,9 +383,10 @@ public static void RegisterAppDomainUnhandledExceptionHandler(this Exceptionless
385383
}
386384

387385
log.Info(typeof(ExceptionlessClient), "AppDomain.CurrentDomain.UnhandledException finished");
388-
log.Flush();
389386
} catch (Exception ex) {
390387
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing AppDomain unhandled exception: ", ex.Message));
388+
} finally {
389+
log.Flush();
391390
}
392391
};
393392
}
@@ -430,9 +429,10 @@ public static void RegisterOnProcessExitHandler(this ExceptionlessClient client)
430429
}
431430

432431
log.Info(typeof(ExceptionlessClient), "ProcessExit finished");
433-
log.Flush();
434432
} catch (Exception ex) {
435433
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing process exit: ", ex.Message));
434+
} finally {
435+
log.Flush();
436436
}
437437
};
438438
}

src/Platforms/Exceptionless.Web/ExceptionlessClientExtensions.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,30 @@ public static void RegisterHttpApplicationErrorHandler(this ExceptionlessClient
1717
if (HttpContext.Current == null)
1818
return;
1919

20-
Exception exception = HttpContext.Current.Server.GetLastError();
20+
var exception = HttpContext.Current.Server.GetLastError();
2121
if (exception == null)
2222
return;
2323

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

29-
exception.ToExceptionless(contextData, client).Submit();
28+
var contextData = new ContextData();
29+
contextData.MarkAsUnhandledError();
30+
contextData.SetSubmissionMethod("HttpApplicationError");
31+
contextData.Add("HttpContext", HttpContext.Current.ToWrapped());
32+
33+
exception.ToExceptionless(contextData, client).Submit();
34+
35+
// process queue immediately since the app is about to exit.
36+
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();
37+
38+
log.Info(typeof(ExceptionlessClient), "HttpApplication.Error finished");
39+
} catch (Exception ex) {
40+
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing HttpApplication unhandled exception: ", ex.Message));
41+
} finally {
42+
log.Flush();
43+
}
3044
};
3145

3246
try {

src/Platforms/Exceptionless.Windows/ExceptionlessClientExtensions.cs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,28 @@ public static void RegisterApplicationThreadExceptionHandler(this ExceptionlessC
1313
if (client == null)
1414
throw new ArgumentNullException(nameof(client));
1515

16-
if (_onApplicationThreadException == null)
16+
if (_onApplicationThreadException == null) {
1717
_onApplicationThreadException = (sender, args) => {
18-
var contextData = new ContextData();
19-
contextData.MarkAsUnhandledError();
20-
contextData.SetSubmissionMethod("ApplicationThreadException");
21-
22-
args.Exception.ToExceptionless(contextData, client).Submit();
18+
var log = client.Configuration.Resolver.GetLog();
19+
try {
20+
log.Info(typeof(ExceptionlessClient), "Application.ThreadException called");
21+
var contextData = new ContextData();
22+
contextData.MarkAsUnhandledError();
23+
contextData.SetSubmissionMethod("ApplicationThreadException");
24+
25+
args.Exception.ToExceptionless(contextData, client).Submit();
26+
27+
// process queue immediately since the app is about to exit.
28+
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();
29+
30+
log.Info(typeof(ExceptionlessClient), "Application.ThreadException finished");
31+
} catch (Exception ex) {
32+
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing Application Thread Exception: ", ex.Message));
33+
} finally {
34+
log.Flush();
35+
}
2336
};
37+
}
2438

2539
try {
2640
Application.ThreadException -= _onApplicationThreadException;

src/Platforms/Exceptionless.Wpf/ExceptionlessClientExtensions.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
using Exceptionless.Plugins;
55
using Exceptionless.Logging;
66

7-
#pragma warning disable AsyncFixer03
8-
97
namespace Exceptionless.Wpf.Extensions {
108
public static class ExceptionlessClientExtensions {
119
private static DispatcherUnhandledExceptionEventHandler _onApplicationDispatcherUnhandledException;
@@ -17,22 +15,30 @@ public static void RegisterApplicationDispatcherUnhandledExceptionHandler(this E
1715
if (System.Windows.Application.Current == null)
1816
return;
1917

20-
if (_onApplicationDispatcherUnhandledException == null)
21-
_onApplicationDispatcherUnhandledException = async (sender, args) => {
22-
var contextData = new ContextData();
23-
contextData.MarkAsUnhandledError();
24-
contextData.SetSubmissionMethod("DispatcherUnhandledException");
25-
26-
args.Exception.ToExceptionless(contextData, client).Submit();
18+
if (_onApplicationDispatcherUnhandledException == null) {
19+
_onApplicationDispatcherUnhandledException = (sender, args) => {
20+
var log = client.Configuration.Resolver.GetLog();
2721

2822
try {
23+
log.Info(typeof(ExceptionlessClient), "Application.Current.DispatcherUnhandledException called");
24+
25+
var contextData = new ContextData();
26+
contextData.MarkAsUnhandledError();
27+
contextData.SetSubmissionMethod("DispatcherUnhandledException");
28+
29+
args.Exception.ToExceptionless(contextData, client).Submit();
30+
2931
// process queue immediately since the app is about to exit.
30-
await client.ProcessQueueAsync().ConfigureAwait(false);
32+
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();
33+
34+
log.Info(typeof(ExceptionlessClient), "Application.Current.DispatcherUnhandledException finished");
3135
} catch (Exception ex) {
32-
var log = client.Configuration.Resolver.GetLog();
3336
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing application dispatcher exception: ", ex.Message));
37+
} finally {
38+
log.Flush();
3439
}
3540
};
41+
}
3642

3743
try {
3844
System.Windows.Application.Current.DispatcherUnhandledException -= _onApplicationDispatcherUnhandledException;

0 commit comments

Comments
 (0)