Skip to content

Commit f92841b

Browse files
Enable .NET analyzers
Enable .NET analyzers and address recommendations to avoid redundant work, formatted strings, incorrect use of `ArgumentNullException` and avoiding an instance method that does not access state.
1 parent e4743cf commit f92841b

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

src/accounting/Consumer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public Consumer(ILogger<Consumer> logger)
3939
_logger = logger;
4040

4141
var servers = Environment.GetEnvironmentVariable("KAFKA_ADDR")
42-
?? throw new ArgumentNullException("KAFKA_ADDR");
42+
?? throw new InvalidOperationException("The KAFKA_ADDR environment variable is not set.");
4343

4444
_consumer = BuildConsumer(servers);
4545
_consumer.Subscribe(TopicName);
4646

47-
_logger.LogInformation($"Connecting to Kafka: {servers}");
47+
_logger.LogInformation("Connecting to Kafka: {servers}", servers);
4848
_dbContext = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING") == null ? null : new DBContext();
4949
}
5050

@@ -64,7 +64,7 @@ public void StartListening()
6464
}
6565
catch (ConsumeException e)
6666
{
67-
_logger.LogError(e, "Consume error: {0}", e.Error.Reason);
67+
_logger.LogError(e, "Consume error: {reason}", e.Error.Reason);
6868
}
6969
}
7070
}
@@ -130,7 +130,7 @@ private void ProcessMessage(Message<string, byte[]> message)
130130
}
131131
}
132132

133-
private IConsumer<string, byte[]> BuildConsumer(string servers)
133+
private static IConsumer<string, byte[]> BuildConsumer(string servers)
134134
{
135135
var conf = new ConsumerConfig
136136
{

src/accounting/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<PropertyGroup Condition="'$(Configuration)'=='Release'">
9+
<AnalysisMode>Minimum</AnalysisMode>
910
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1011
</PropertyGroup>
1112
</Project>

src/cart/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
</PropertyGroup>
77

88
<PropertyGroup Condition="'$(Configuration)'=='Release'">
9+
<AnalysisMode>Minimum</AnalysisMode>
910
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
1011
</PropertyGroup>
1112
</Project>

src/cart/src/cartstore/ValkeyCartStore.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ private void EnsureRedisConnected()
8686
return;
8787
}
8888

89-
_logger.LogDebug("Connecting to Redis: {_connectionString}", _connectionString);
89+
if (_logger.IsEnabled(LogLevel.Debug))
90+
{
91+
_logger.LogDebug("Connecting to Redis: {_connectionString}", _connectionString);
92+
}
93+
9094
_redis = ConnectionMultiplexer.Connect(_redisConnectionOptions);
9195

9296
if (_redis == null || !_redis.IsConnected)
@@ -103,7 +107,11 @@ private void EnsureRedisConnected()
103107
_logger.LogDebug("Performing small test");
104108
cache.StringSet("cart", "OK" );
105109
object res = cache.StringGet("cart");
106-
_logger.LogDebug("Small test result: {res}", res);
110+
111+
if (_logger.IsEnabled(LogLevel.Debug))
112+
{
113+
_logger.LogDebug("Small test result: {res}", res);
114+
}
107115

108116
_redis.InternalError += (_, e) => { Console.WriteLine(e.Exception); };
109117
_redis.ConnectionRestored += (_, _) =>
@@ -124,7 +132,11 @@ private void EnsureRedisConnected()
124132
public async Task AddItemAsync(string userId, string productId, int quantity)
125133
{
126134
var stopwatch = Stopwatch.StartNew();
127-
_logger.LogInformation($"AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}");
135+
136+
if (_logger.IsEnabled(LogLevel.Information))
137+
{
138+
_logger.LogInformation("AddItemAsync called with userId={userId}, productId={productId}, quantity={quantity}", userId, productId, quantity);
139+
}
128140

129141
try
130142
{
@@ -173,8 +185,10 @@ public async Task AddItemAsync(string userId, string productId, int quantity)
173185

174186
public async Task EmptyCartAsync(string userId)
175187
{
176-
_logger.LogInformation($"EmptyCartAsync called with userId={userId}");
177-
188+
if (_logger.IsEnabled(LogLevel.Information))
189+
{
190+
_logger.LogInformation("EmptyCartAsync called with userId={userId}", userId);
191+
}
178192
try
179193
{
180194
EnsureRedisConnected();
@@ -193,7 +207,11 @@ public async Task EmptyCartAsync(string userId)
193207
public async Task<Oteldemo.Cart> GetCartAsync(string userId)
194208
{
195209
var stopwatch = Stopwatch.StartNew();
196-
_logger.LogInformation($"GetCartAsync called with userId={userId}");
210+
211+
if (_logger.IsEnabled(LogLevel.Information))
212+
{
213+
_logger.LogInformation("GetCartAsync called with userId={userId}", userId);
214+
}
197215

198216
try
199217
{

0 commit comments

Comments
 (0)