Skip to content

Commit 61e3291

Browse files
authored
Fixed issues with async event handlers and improved trace logging (#306)
* Fixed issues with async event handlers and improved trace logging * Ensure flush is called on shutdown
1 parent eab5003 commit 61e3291

File tree

4 files changed

+58
-19
lines changed

4 files changed

+58
-19
lines changed

src/Exceptionless/Configuration/SettingsManager.cs

+14-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public static async Task UpdateSettingsAsync(ExceptionlessConfiguration config,
7575
if (config == null || !config.IsValid || !config.Enabled || _isUpdatingSettings)
7676
return;
7777

78+
var log = config.Resolver.GetLog();
79+
7880
try {
7981
_isUpdatingSettings = true;
8082
if (!version.HasValue || version < 0)
@@ -84,8 +86,18 @@ public static async Task UpdateSettingsAsync(ExceptionlessConfiguration config,
8486
var client = config.Resolver.GetSubmissionClient();
8587

8688
var response = await client.GetSettingsAsync(config, version.Value, serializer).ConfigureAwait(false);
87-
if (!response.Success || response.Settings == null)
89+
if (!response.Success) {
90+
string message = String.IsNullOrEmpty(response.Message)
91+
? "An error occurred retrieving configuration settings."
92+
: String.Concat("An error occurred retrieving configuration settings: ", response.Message);
93+
log.Error(typeof(SettingsManager), response.Exception, message);
94+
return;
95+
}
96+
97+
if (response.Settings == null) {
98+
log.Warn(typeof(SettingsManager), "Error occurred updating settings: Server settings was null");
8899
return;
100+
}
89101

90102
var savedServerSettings = GetSavedServerSettings(config);
91103
config.Settings.Apply(response.Settings);
@@ -101,7 +113,7 @@ public static async Task UpdateSettingsAsync(ExceptionlessConfiguration config,
101113
var fileStorage = config.Resolver.GetFileStorage();
102114
fileStorage.SaveObject(GetConfigPath(config), response.Settings);
103115
} catch (Exception ex) {
104-
config.Resolver.GetLog().Error(typeof(SettingsManager), ex, "Error occurred updating settings.");
116+
log.Error(typeof(SettingsManager), ex, "Error occurred updating settings.");
105117
} finally {
106118
_isUpdatingSettings = false;
107119
}

src/Exceptionless/ExceptionlessClient.cs

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public Task ProcessQueueAsync() {
148148
}
149149
}
150150

151+
_log.Value.Info(typeof(ExceptionlessClient), "Processing event queue");
151152
return _queue.Value.ProcessAsync();
152153
}
153154

src/Exceptionless/Extensions/ExceptionlessClientExtensions.cs

+38-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public static void Startup(this ExceptionlessClient client, string apiKey = null
2828
if (client.Configuration.UpdateSettingsWhenIdleInterval == null)
2929
client.Configuration.UpdateSettingsWhenIdleInterval = TimeSpan.FromMinutes(2);
3030

31+
var log = client.Configuration.Resolver.GetLog();
32+
log.FormattedInfo(typeof(ExceptionlessClient), "Startup called ApiKey={0} ServerUrl={1}", client.Configuration.ApiKey, client.Configuration.ServerUrl);
33+
3134
client.RegisterAppDomainUnhandledExceptionHandler();
3235

3336
// make sure that queued events are sent when the app exits
@@ -36,6 +39,8 @@ public static void Startup(this ExceptionlessClient client, string apiKey = null
3639

3740
if (client.Configuration.SessionsEnabled)
3841
client.SubmitSessionStart();
42+
43+
log.Info(typeof(ExceptionlessClient), "Startup finished");
3944
}
4045

4146
/// <summary>
@@ -46,13 +51,21 @@ public static async Task ShutdownAsync(this ExceptionlessClient client) {
4651
if (client == null)
4752
throw new ArgumentNullException(nameof(client));
4853

54+
var log = client.Configuration.Resolver.GetLog();
55+
log.Info(typeof(ExceptionlessClient), "Shutdown called");
56+
4957
client.UnregisterAppDomainUnhandledExceptionHandler();
5058
client.UnregisterOnProcessExitHandler();
5159
client.UnregisterTaskSchedulerUnobservedTaskExceptionHandler();
5260

5361
await client.ProcessQueueAsync().ConfigureAwait(false);
54-
if (client.Configuration.SessionsEnabled)
62+
if (client.Configuration.SessionsEnabled) {
63+
log.Info(typeof(ExceptionlessClient), "Sending Session End Heartbeat");
5564
await client.SubmitSessionEndAsync().ConfigureAwait(false);
65+
}
66+
67+
log.Info(typeof(ExceptionlessClient), "Shutdown finished");
68+
log.Flush();
5669
}
5770

5871
#region Submission Extensions
@@ -348,25 +361,32 @@ public static void RegisterAppDomainUnhandledExceptionHandler(this Exceptionless
348361
throw new ArgumentNullException(nameof(client));
349362

350363
if (_onAppDomainUnhandledException == null) {
351-
_onAppDomainUnhandledException = async (sender, args) => {
364+
_onAppDomainUnhandledException = (sender, args) => {
352365
var exception = args.ExceptionObject as Exception;
353366
if (exception == null)
354367
return;
355368

369+
var log = client.Configuration.Resolver.GetLog();
356370
try {
371+
log.Info(typeof(ExceptionlessClient), "AppDomain.CurrentDomain.UnhandledException called");
372+
357373
var contextData = new ContextData();
358374
contextData.MarkAsUnhandledError();
359375
contextData.SetSubmissionMethod("AppDomainUnhandledException");
360376

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

363379
// process queue immediately since the app is about to exit.
364-
await client.ProcessQueueAsync().ConfigureAwait(false);
380+
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();
381+
382+
if (client.Configuration.SessionsEnabled) {
383+
log.Info(typeof(ExceptionlessClient), "Sending Session End Heartbeat");
384+
client.SubmitSessionEndAsync().ConfigureAwait(false).GetAwaiter().GetResult();
385+
}
365386

366-
if (client.Configuration.SessionsEnabled)
367-
await client.SubmitSessionEndAsync().ConfigureAwait(false);
387+
log.Info(typeof(ExceptionlessClient), "AppDomain.CurrentDomain.UnhandledException finished");
388+
log.Flush();
368389
} catch (Exception ex) {
369-
var log = client.Configuration.Resolver.GetLog();
370390
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing AppDomain unhandled exception: ", ex.Message));
371391
}
372392
};
@@ -397,14 +417,21 @@ public static void RegisterOnProcessExitHandler(this ExceptionlessClient client)
397417
throw new ArgumentNullException(nameof(client));
398418

399419
if (_onProcessExit == null) {
400-
_onProcessExit = async (sender, args) => {
420+
_onProcessExit = (sender, args) => {
421+
var log = client.Configuration.Resolver.GetLog();
401422
try {
402-
await client.ProcessQueueAsync().ConfigureAwait(false);
423+
log.Info(typeof(ExceptionlessClient), "ProcessExit called");
424+
425+
client.ProcessQueueAsync().ConfigureAwait(false).GetAwaiter().GetResult();
426+
427+
if (client.Configuration.SessionsEnabled) {
428+
log.Info(typeof(ExceptionlessClient), "Sending Session End Heartbeat");
429+
client.SubmitSessionEndAsync().ConfigureAwait(false).GetAwaiter().GetResult();
430+
}
403431

404-
if (client.Configuration.SessionsEnabled)
405-
await client.SubmitSessionEndAsync().ConfigureAwait(false);
432+
log.Info(typeof(ExceptionlessClient), "ProcessExit finished");
433+
log.Flush();
406434
} catch (Exception ex) {
407-
var log = client.Configuration.Resolver.GetLog();
408435
log.Error(typeof(ExceptionlessClientExtensions), ex, String.Concat("An error occurred while processing process exit: ", ex.Message));
409436
}
410437
};

src/Exceptionless/Submission/DefaultSubmissionClient.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task<SubmissionResponse> PostEventsAsync(IEnumerable<Event> events,
4343
_client.Value.AddAuthorizationHeader(config.ApiKey);
4444
response = await _client.Value.PostAsync(url, content).ConfigureAwait(false);
4545
} catch (Exception ex) {
46-
return new SubmissionResponse(500, exception: ex);
46+
return new SubmissionResponse(500, message: ex.GetMessage(), exception: ex);
4747
}
4848

4949
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out int settingsVersion))
@@ -75,7 +75,7 @@ public async Task<SubmissionResponse> PostUserDescriptionAsync(string referenceI
7575
_client.Value.AddAuthorizationHeader(config.ApiKey);
7676
response = await _client.Value.PostAsync(url, content).ConfigureAwait(false);
7777
} catch (Exception ex) {
78-
return new SubmissionResponse(500, exception: ex);
78+
return new SubmissionResponse(500, message: ex.GetMessage(), exception: ex);
7979
}
8080

8181
if (Int32.TryParse(GetSettingsVersionHeader(response.Headers), out int settingsVersion))
@@ -100,16 +100,15 @@ public async Task<SettingsResponse> GetSettingsAsync(ExceptionlessConfiguration
100100
_client.Value.AddAuthorizationHeader(config.ApiKey);
101101
response = await _client.Value.GetAsync(url).ConfigureAwait(false);
102102
} catch (Exception ex) {
103-
var message = String.Concat("Unable to retrieve configuration settings. Exception: ", ex.GetMessage());
104-
return new SettingsResponse(false, message: message);
103+
return new SettingsResponse(false, message: ex.GetMessage(), exception: ex);
105104
}
106105

107106
if (response != null && response.StatusCode == HttpStatusCode.NotModified)
108107
return SettingsResponse.NotModified;
109108

110109
if (response == null || response.StatusCode != HttpStatusCode.OK) {
111110
string message = await GetResponseMessageAsync(response).ConfigureAwait(false);
112-
return new SettingsResponse(false, message: String.Concat("Unable to retrieve configuration settings: ", message));
111+
return new SettingsResponse(false, message: message);
113112
}
114113

115114
string json = await GetResponseTextAsync(response).ConfigureAwait(false);
@@ -130,7 +129,7 @@ public async Task SendHeartbeatAsync(string sessionIdOrUserId, bool closeSession
130129
await _client.Value.GetAsync(url).ConfigureAwait(false);
131130
} catch (Exception ex) {
132131
var log = config.Resolver.GetLog();
133-
log.Error(String.Concat("Error submitting heartbeat: ", ex.GetMessage()));
132+
log.Error(String.Concat("Error submitting heartbeat: ", ex.GetMessage()), exception: ex);
134133
}
135134
}
136135

0 commit comments

Comments
 (0)