private async Task FlushEventsAsync(FlushPayload payload) { EventOutputFormatter formatter = new EventOutputFormatter(_config); string jsonEvents; int eventCount; const int maxAttempts = 2; try { jsonEvents = formatter.SerializeOutputEvents(payload.Events, payload.Summary, out eventCount); } catch (Exception e) { DefaultEventProcessor.Log.ErrorFormat("Error preparing events, will not send: {0}", e, Util.ExceptionMessage(e)); return; } string payloadId = Guid.NewGuid().ToString(); for (var attempt = 0; attempt < maxAttempts; attempt++) { if (attempt > 0) { await Task.Delay(TimeSpan.FromSeconds(1)); } using (var cts = new CancellationTokenSource(_config.HttpClientTimeout)) { string errorMessage = null; bool canRetry = false; try { await SendEventsAsync(jsonEvents, eventCount, payloadId, cts.Token); return; // success } catch (TaskCanceledException e) { if (e.CancellationToken == cts.Token) { // Indicates the task was cancelled deliberately somehow; in this case don't retry DefaultEventProcessor.Log.Warn("Event sending task was cancelled"); return; } else { // Otherwise this was a request timeout. errorMessage = "Timed out"; canRetry = true; } } catch (UnsuccessfulResponseException e) { errorMessage = Util.HttpErrorMessageBase(e.StatusCode); if (Util.IsHttpErrorRecoverable(e.StatusCode)) { canRetry = true; } else { _disabled = true; // for error 401, etc. } } catch (Exception e) { errorMessage = string.Format("Error ({0})", Util.DescribeException(e)); canRetry = true; } string nextStepDesc = canRetry ? (maxAttempts == maxAttempts - 1 ? "will not retry" : "will retry after one second") : "giving up permanently"; DefaultEventProcessor.Log.WarnFormat(errorMessage + " sending {0} event(s); {1}", eventCount, nextStepDesc); if (!canRetry) { return; } } } }
private async Task FlushEventsAsync(FlushPayload payload) { EventOutputFormatter formatter = new EventOutputFormatter(_config); string jsonEvents; int eventCount; const int maxAttempts = 2; try { jsonEvents = formatter.SerializeOutputEvents(payload.Events, payload.Summary, out eventCount); } catch (Exception e) { DefaultEventProcessor.Log.ErrorFormat("Error preparing events, will not send: {0}", e, Util.ExceptionMessage(e)); return; } for (var attempt = 0; attempt < maxAttempts; attempt++) { if (attempt > 0) { await Task.Delay(TimeSpan.FromSeconds(1)); } using (var cts = new CancellationTokenSource(_config.HttpClientTimeout)) { try { await SendEventsAsync(jsonEvents, eventCount, cts.Token); return; // success } catch (Exception e) { var errorMessage = "Error ({2})"; switch (e) { case TaskCanceledException tce: if (tce.CancellationToken == cts.Token) { // Indicates the task was cancelled deliberately somehow; in this case don't retry DefaultEventProcessor.Log.Warn("Event sending task was cancelled"); return; } else { // Otherwise this was a request timeout. errorMessage = "Timed out"; } break; default: break; } DefaultEventProcessor.Log.WarnFormat(errorMessage + " sending {0} event(s); {1}", eventCount, attempt == maxAttempts - 1 ? "will not retry" : "will retry after one second", Util.ExceptionMessage(e)); } } } }