Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/accounting/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public Consumer(ILogger<Consumer> 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();
}

Expand All @@ -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);
}
}
}
}
Expand Down Expand Up @@ -130,7 +137,7 @@ private void ProcessMessage(Message<string, byte[]> message)
}
}

private IConsumer<string, byte[]> BuildConsumer(string servers)
private static IConsumer<string, byte[]> BuildConsumer(string servers)
{
var conf = new ConsumerConfig
{
Expand Down
1 change: 1 addition & 0 deletions src/accounting/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<AnalysisMode>Minimum</AnalysisMode>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions src/cart/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<AnalysisMode>Minimum</AnalysisMode>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
</Project>
30 changes: 24 additions & 6 deletions src/cart/src/cartstore/ValkeyCartStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 += (_, _) =>
Expand All @@ -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
{
Expand Down Expand Up @@ -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();
Expand All @@ -193,7 +207,11 @@ public async Task EmptyCartAsync(string userId)
public async Task<Oteldemo.Cart> 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
{
Expand Down