private async Task Execute() { if (Verbose) { Trace.WriteLine("Execution of writer loop started. Waiting for log lines..."); } while (!_cancellationTokenSource.Token.IsCancellationRequested) { // wait for processing if (Verbose) { Trace.WriteLine($"Loop waiting for {LogDnaConfig.WriterLoopInterval} ms"); } await Task.Delay(LogDnaConfig.WriterLoopInterval); if (!HasLines()) { if (Verbose) { Trace.WriteLine($"No lines this time... see you in {LogDnaConfig.WriterLoopInterval} ms"); } continue; } LogLineBatch logLinesBatch; lock (_linesLock) { var currentLogLines = new LogLine[_logLines.Count]; _logLines.CopyTo(currentLogLines, 0); _logLines.Clear(); logLinesBatch = new LogLineBatch(currentLogLines); } if (Verbose) { Trace.WriteLine($"Prepared batch with {logLinesBatch.Lines.Count()} log lines"); } await SendBatchWithRetry(logLinesBatch); } if (Verbose) { Trace.WriteLine("Stopping execution of writer loop. Good bye!"); } }
private async Task SendBatchWithRetry(LogLineBatch logLineBatch) { var tries = 1; var successful = false; do { var request = new IngestRequest() { HostName = _dnsInfoProvider.GetHostName() }; var response = await _logDnaApi.Ingest(request, LogDnaConfig.AuthorizationHeader, logLineBatch); successful = response.IsSuccessStatusCode; if (!successful) { var errorMessage = await response.Content.ReadAsStringAsync(); await Console.Error.WriteAsync( $"Failed to log batch of {logLineBatch.Lines.Count()} log lines: {response.StatusCode} - {errorMessage}" ); } else if (Verbose) { Trace.WriteLine( $"Successfully sent batch of {logLineBatch.Lines.Count()} on attempt {tries}: : {response.StatusCode}" ); } } while (!successful && tries++ <= LogDnaConfig.WriterMaxRetries); if (!successful) { await Console.Error.WriteAsync( $"Failed to log batch of {logLineBatch.Lines.Count()} log lines after {LogDnaConfig.WriterMaxRetries} attempts" ); } }