Skip to content

Commit d008bd6

Browse files
committed
Ensure that we are only processing the queue once on process exit.
1 parent 4de2c48 commit d008bd6

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

Source/Platforms/Console/ExceptionlessConsoleExtensions.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,43 @@
88
#endregion
99

1010
using System;
11+
using Exceptionless.Dependency;
12+
using Exceptionless.Logging;
1113

1214
namespace Exceptionless {
1315
public static class ExceptionlessConsoleExtensions {
16+
private static EventHandler _onProcessExit;
17+
1418
public static void Register(this ExceptionlessClient client) {
1519
client.Startup();
1620

1721
// make sure that queued events are sent when the app exits
18-
AppDomain.CurrentDomain.ProcessExit += (sender, args) => {
19-
client.ProcessQueue();
20-
};
22+
client.RegisterOnProcessExitHandler();
2123
}
2224

2325
public static void Unregister(this ExceptionlessClient client) {
2426
client.Shutdown();
27+
client.UnregisterOnProcessExitHandler();
28+
}
29+
30+
private static void RegisterOnProcessExitHandler(this ExceptionlessClient client) {
31+
if (_onProcessExit == null)
32+
_onProcessExit = (sender, args) => client.ProcessQueue();
33+
34+
try {
35+
AppDomain.CurrentDomain.ProcessExit -= _onProcessExit;
36+
AppDomain.CurrentDomain.ProcessExit += _onProcessExit;
37+
} catch (Exception ex) {
38+
client.Configuration.Resolver.GetLog().Error(typeof(ExceptionlessClientExtensions), ex, "An error occurred while wiring up to the process exit event.");
39+
}
40+
}
41+
42+
private static void UnregisterOnProcessExitHandler(this ExceptionlessClient client) {
43+
if (_onProcessExit == null)
44+
return;
45+
46+
AppDomain.CurrentDomain.ProcessExit -= _onProcessExit;
47+
_onProcessExit = null;
2548
}
2649
}
2750
}

0 commit comments

Comments
 (0)