public async Task PollAsync(CancellationToken cancellationToken) { var receiveMessageRequest = new ReceiveMessageRequest { MaxNumberOfMessages = 1, MessageAttributeNames = { "All" }, QueueUrl = queuePollingOptions.QueueIdentifier, VisibilityTimeout = queuePollingOptions.MessageVisibilityTimeoutInSeconds == 0 ? 43200 : queuePollingOptions.MessageVisibilityTimeoutInSeconds, WaitTimeSeconds = queuePollingOptions.PollingWaitTimeInSeconds }; while (!cancellationToken.IsCancellationRequested) { try { logger.LogDebug($"Polling queue: {queuePollingOptions.QueueIdentifier}"); var receiveMessageResponse = await amazonSqs.ReceiveMessageAsync(receiveMessageRequest, cancellationToken); foreach (var message in receiveMessageResponse.Messages) { var correlationId = GetCorrelationId(message); using (logger.BeginScope(new Dictionary <string, object> { [MessageAttributes.CorrelationId] = correlationId })) { try { logger.LogDebug($"Despatching message: {message.MessageId}"); await messageDespatcher.DespatchAsync(correlationId, message); logger.LogDebug($"Deleting message: {message.MessageId}"); await amazonSqs.DeleteMessageAsync(queuePollingOptions.QueueIdentifier, message.ReceiptHandle, cancellationToken); } catch (Exception exception) { await UnlockMessageAsync(message, cancellationToken); logger.LogError(exception, $"Error while handling message: {message.MessageId}"); } } } } catch (Exception exception) { logger.LogError(exception, $"Error while polling queue: {queuePollingOptions.QueueIdentifier}"); } } }
protected override void Write(LogEventInfo logEvent) { if (_messageDespatcher == null) { throw new ArgumentNullException(nameof(_messageDespatcher)); } if (logEvent?.LoggerName?.Equals("Amazon", StringComparison.InvariantCultureIgnoreCase) ?? false) //prevent an infinite loop { return; } var message = Layout.Render(logEvent); _messageDespatcher.DespatchAsync(GetTopicArn(), message).ConfigureAwait(false).GetAwaiter().GetResult(); }