diff --git a/src/accounting/Consumer.cs b/src/accounting/Consumer.cs index 89ede49ee9..af54c58f21 100644 --- a/src/accounting/Consumer.cs +++ b/src/accounting/Consumer.cs @@ -39,12 +39,16 @@ public Consumer(ILogger logger) _logger = logger; var servers = Environment.GetEnvironmentVariable("KAFKA_ADDR") - ?? throw new ArgumentNullException("KAFKA_ADDR"); + ?? throw new InvalidOperationException("The KAFKA_ADDR environment variable is not set."); _consumer = BuildConsumer(servers); _consumer.Subscribe(TopicName); - _logger.LogInformation($"Connecting to Kafka: {servers}"); + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("Connecting to Kafka: {servers}", servers); + } + _dbContext = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") == null ? null : new DBContext(); } @@ -64,7 +68,10 @@ public void StartListening() } catch (ConsumeException e) { - _logger.LogError(e, "Consume error: {0}", e.Error.Reason); + if (_logger.IsEnabled(LogLevel.Error)) + { + _logger.LogError(e, "Consume error: {reason}", e.Error.Reason); + } } } } @@ -130,7 +137,7 @@ private void ProcessMessage(Message message) } } - private IConsumer BuildConsumer(string servers) + private static IConsumer BuildConsumer(string servers) { var conf = new ConsumerConfig { diff --git a/src/accounting/Directory.Build.props b/src/accounting/Directory.Build.props index bfd1cab4dd..92cd6f4444 100644 --- a/src/accounting/Directory.Build.props +++ b/src/accounting/Directory.Build.props @@ -6,6 +6,7 @@ + Minimum true diff --git a/src/cart/Directory.Build.props b/src/cart/Directory.Build.props index bfd1cab4dd..92cd6f4444 100644 --- a/src/cart/Directory.Build.props +++ b/src/cart/Directory.Build.props @@ -6,6 +6,7 @@ + Minimum true diff --git a/src/cart/src/cartstore/ValkeyCartStore.cs b/src/cart/src/cartstore/ValkeyCartStore.cs index 8b230baaa4..17f9b05f15 100644 --- a/src/cart/src/cartstore/ValkeyCartStore.cs +++ b/src/cart/src/cartstore/ValkeyCartStore.cs @@ -86,7 +86,11 @@ private void EnsureRedisConnected() return; } - _logger.LogDebug("Connecting to Redis: {_connectionString}", _connectionString); + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug("Connecting to Redis: {connectionString}", _connectionString); + } + _redis = ConnectionMultiplexer.Connect(_redisConnectionOptions); if (_redis == null || !_redis.IsConnected) @@ -103,7 +107,11 @@ private void EnsureRedisConnected() _logger.LogDebug("Performing small test"); cache.StringSet("cart", "OK" ); object res = cache.StringGet("cart"); - _logger.LogDebug("Small test result: {res}", res); + + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug("Small test result: {result}", res); + } _redis.InternalError += (_, e) => { Console.WriteLine(e.Exception); }; _redis.ConnectionRestored += (_, _) => @@ -124,7 +132,11 @@ private void EnsureRedisConnected() public async Task AddItemAsync(string userId, string productId, int quantity) { var stopwatch = Stopwatch.StartNew(); - _logger.LogInformation($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}"); + + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}", userId, productId, quantity); + } try { @@ -173,8 +185,10 @@ public async Task AddItemAsync(string userId, string productId, int quantity) public async Task EmptyCartAsync(string userId) { - _logger.LogInformation($"EmptyCartAsync called with userId={userId}"); - + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("EmptyCartAsync called with userId={userId}", userId); + } try { EnsureRedisConnected(); @@ -193,7 +207,11 @@ public async Task EmptyCartAsync(string userId) public async Task GetCartAsync(string userId) { var stopwatch = Stopwatch.StartNew(); - _logger.LogInformation($"GetCartAsync called with userId={userId}"); + + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("GetCartAsync called with userId={userId}", userId); + } try {