private async Task ConsumeInternalAsync(IKafkaConsumer kafkaConsumer, CancellationToken cancellationToken) { var builder = new ConsumerBuilder <string, string>(_configOptions.ConsumerConfig()); using IConsumer <string, string> consumer = builder.Build(); try { consumer.Subscribe(kafkaConsumer.Topic); try { while (!_cancelled && !cancellationToken.IsCancellationRequested) { ConsumeResult <string, string> consumed = consumer.Consume(cancellationToken); _logger.LogInformation($"{kafkaConsumer.GetType().Name}: consumed"); await kafkaConsumer.ConsumeAsync(consumed.Message.Value); } } catch (ConsumeException e) { _logger.LogError(e, kafkaConsumer.Topic + $"\r\nAn exception during consuming\r\n" + $"Reason: {e.Error.Reason}\r\n" + $"Consumer is being closed"); } } catch (Exception exception) { _logger.LogError( exception, $"Topic: {kafkaConsumer.Topic}\r\n" + $"An exception during consuming\r\n"); } finally { _logger.LogInformation($"{kafkaConsumer.Topic}. Consumer is being closed"); consumer.Close(); } }