Skip to content

Commit 90059f0

Browse files
committed
Fixed a null reference exception that could occur if the configuration was locked.
1 parent f3cd980 commit 90059f0

File tree

6 files changed

+65
-14
lines changed

6 files changed

+65
-14
lines changed

Source/Platforms/Console/ExceptionlessConsoleExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private static void RegisterOnProcessExitHandler(this ExceptionlessClient client
3535
AppDomain.CurrentDomain.ProcessExit -= _onProcessExit;
3636
AppDomain.CurrentDomain.ProcessExit += _onProcessExit;
3737
} catch (Exception ex) {
38-
client.Configuration.Resolver.GetLog().Error(typeof(ExceptionlessClientExtensions), ex, "An error occurred while wiring up to the process exit event.");
38+
client.Configuration.Resolver.GetLog().Error(typeof(ExceptionlessConsoleExtensions), ex, "An error occurred while wiring up to the process exit event.");
3939
}
4040
}
4141

Source/Platforms/Windows/ExceptionlessClientExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ public static void RegisterApplicationThreadExceptionHandler(this ExceptionlessC
2525
} catch (Exception ex) {
2626
client.Configuration.Resolver.GetLog().Error(typeof(ExceptionlessClientExtensions), ex, "An error occurred while wiring up to the unobserved task exception event.");
2727
}
28-
29-
// make sure that queued events are sent when the app exits
30-
AppDomain.CurrentDomain.ProcessExit += (sender, args) => {
31-
client.ProcessQueue();
32-
};
3328
}
3429

3530
public static void UnregisterApplicationThreadExceptionHandler(this ExceptionlessClient client) {

Source/Platforms/Windows/ExceptionlessWindowsExtensions.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99

1010
using System;
1111
using System.Windows.Forms;
12+
using Exceptionless.Dependency;
1213
using Exceptionless.Dialogs;
14+
using Exceptionless.Logging;
1315
using Exceptionless.Windows.Extensions;
1416

1517
namespace Exceptionless {
1618
public static class ExceptionlessWindowsExtensions {
19+
private static EventHandler _onProcessExit;
1720
public static void Register(this ExceptionlessClient client, bool showDialog = true) {
1821
client.Startup();
1922
client.RegisterApplicationThreadExceptionHandler();
2023

24+
// make sure that queued events are sent when the app exits
25+
client.RegisterOnProcessExitHandler();
26+
2127
if (!showDialog)
2228
return;
2329

@@ -28,6 +34,7 @@ public static void Register(this ExceptionlessClient client, bool showDialog = t
2834
public static void Unregister(this ExceptionlessClient client) {
2935
client.Shutdown();
3036
client.UnregisterApplicationThreadExceptionHandler();
37+
client.UnregisterOnProcessExitHandler();
3138

3239
client.SubmittingEvent -= OnSubmittingEvent;
3340
}
@@ -41,5 +48,25 @@ private static void OnSubmittingEvent(object sender, EventSubmittingEventArgs e)
4148
var dialog = new CrashReportForm(e.Client, e.Event);
4249
e.Cancel = dialog.ShowDialog() != DialogResult.OK;
4350
}
51+
52+
private static void RegisterOnProcessExitHandler(this ExceptionlessClient client) {
53+
if (_onProcessExit == null)
54+
_onProcessExit = (sender, args) => client.ProcessQueue();
55+
56+
try {
57+
AppDomain.CurrentDomain.ProcessExit -= _onProcessExit;
58+
AppDomain.CurrentDomain.ProcessExit += _onProcessExit;
59+
} catch (Exception ex) {
60+
client.Configuration.Resolver.GetLog().Error(typeof(ExceptionlessWindowsExtensions), ex, "An error occurred while wiring up to the process exit event.");
61+
}
62+
}
63+
64+
private static void UnregisterOnProcessExitHandler(this ExceptionlessClient client) {
65+
if (_onProcessExit == null)
66+
return;
67+
68+
AppDomain.CurrentDomain.ProcessExit -= _onProcessExit;
69+
_onProcessExit = null;
70+
}
4471
}
4572
}

Source/Platforms/Wpf/ExceptionlessClientExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ public static void RegisterApplicationThreadExceptionHandler(this ExceptionlessC
2525
} catch (Exception ex) {
2626
client.Configuration.Resolver.GetLog().Error(typeof(ExceptionlessClientExtensions), ex, "An error occurred while wiring up to the unobserved task exception event.");
2727
}
28-
29-
// make sure that queued events are sent when the app exits
30-
AppDomain.CurrentDomain.ProcessExit += (sender, args) => {
31-
client.ProcessQueue();
32-
};
3328
}
3429

3530
public static void UnregisterApplicationThreadExceptionHandler(this ExceptionlessClient client) {

Source/Platforms/Wpf/ExceptionlessWpfExtensions.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@
1010
using System;
1111
using System.Windows;
1212
using System.Windows.Threading;
13+
using Exceptionless.Dependency;
1314
using Exceptionless.Dialogs;
15+
using Exceptionless.Logging;
1416
using Exceptionless.Wpf.Extensions;
1517

1618
namespace Exceptionless {
1719
public static class ExceptionlessWpfExtensions {
20+
private static EventHandler _onProcessExit;
21+
1822
public static void Register(this ExceptionlessClient client, bool showDialog = true) {
1923
client.Startup();
2024
client.RegisterApplicationThreadExceptionHandler();
2125
client.RegisterApplicationDispatcherUnhandledExceptionHandler();
2226

27+
// make sure that queued events are sent when the app exits
28+
client.RegisterOnProcessExitHandler();
29+
2330
if (!showDialog)
2431
return;
2532

@@ -31,6 +38,7 @@ public static void Unregister(this ExceptionlessClient client) {
3138
client.Shutdown();
3239
client.UnregisterApplicationThreadExceptionHandler();
3340
client.UnregisterApplicationDispatcherUnhandledExceptionHandler();
41+
client.UnregisterOnProcessExitHandler();
3442

3543
client.SubmittingEvent -= OnSubmittingEvent;
3644
}
@@ -45,11 +53,31 @@ private static void OnSubmittingEvent(object sender, EventSubmittingEventArgs e)
4553
else
4654
e.Cancel = !ShowDialog(e);
4755
}
48-
56+
4957
private static bool ShowDialog(EventSubmittingEventArgs e) {
5058
var dialog = new CrashReportDialog(e.Client, e.Event);
5159
bool? result = dialog.ShowDialog();
5260
return result.HasValue && result.Value;
5361
}
62+
63+
private static void RegisterOnProcessExitHandler(this ExceptionlessClient client) {
64+
if (_onProcessExit == null)
65+
_onProcessExit = (sender, args) => client.ProcessQueue();
66+
67+
try {
68+
AppDomain.CurrentDomain.ProcessExit -= _onProcessExit;
69+
AppDomain.CurrentDomain.ProcessExit += _onProcessExit;
70+
} catch (Exception ex) {
71+
client.Configuration.Resolver.GetLog().Error(typeof(ExceptionlessWpfExtensions), ex, "An error occurred while wiring up to the process exit event.");
72+
}
73+
}
74+
75+
private static void UnregisterOnProcessExitHandler(this ExceptionlessClient client) {
76+
if (_onProcessExit == null)
77+
return;
78+
79+
AppDomain.CurrentDomain.ProcessExit -= _onProcessExit;
80+
_onProcessExit = null;
81+
}
5482
}
5583
}

Source/Shared/Configuration/ExceptionlessConfiguration.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ internal void LockConfig() {
5252
public string ServerUrl {
5353
get { return _serverUrl; }
5454
set {
55-
if (_configLocked && !_serverUrl.Equals(value))
55+
if (_serverUrl == value)
56+
return;
57+
58+
if (_configLocked)
5659
throw new ArgumentException("ServerUrl can't be changed after the client has been initialized.");
5760

5861
_serverUrl = value;
@@ -70,7 +73,10 @@ public string ServerUrl {
7073
public string ApiKey {
7174
get { return _apiKey; }
7275
set {
73-
if (_configLocked && !_apiKey.Equals(value))
76+
if (_apiKey == value)
77+
return;
78+
79+
if (_configLocked)
7480
throw new ArgumentException("ApiKey can't be changed after the client has been initialized.");
7581

7682
_apiKey = value;

0 commit comments

Comments
 (0)