public async Task <ConsumerResult> Invoke(Message message, Func <Tuple <Message, LogEntry>, Task <ConsumerResult> > next) { var logEntry = _logEntryFactory.Init(new StaticTracingScope { ScopeId = Guid.NewGuid().ToString(), CorrelationId = message.CorrelationId ?? GetHeader(message, "X-Correlation-Id") ?? Guid.NewGuid().ToString(), ParentScopeId = GetHeader(message, "X-Parent-Scope-Id") }); logEntry.AddHeader("RabbitMQMessageId", message.MessageId); logEntry.Route = message.QueueName; logEntry.Protocol = "rabbitmq"; logEntry.RabbitMQ = new LogEntryRabbitMQData { QueueName = message.QueueName, MessageId = message.MessageId, Acked = false }; try { var res = await next(Tuple.Create(message, logEntry)); logEntry.RabbitMQ.Acked = res == ConsumerResult.Ack; return(res); } catch (Exception e) { logEntry.Exception("Uncaught exception", e); throw; } finally { logEntry.Finalize(); foreach (var logger in _loggers) { #pragma warning disable 4014 logger .Publish(logEntry) .ContinueWith((t) => { var e = t.Exception; System.Console.Error.WriteLine($"Error when publishing log entry: {e.Message}"); System.Console.Error.WriteLine(e.StackTrace); }, TaskContinuationOptions.OnlyOnFaulted); #pragma warning restore 4014 } } }