Skip to content

Commit 8aef630

Browse files
authored
Add code example for CA2253 rule (#49089) (#49090)
1 parent ad90c00 commit 8aef630

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

docs/fundamentals/code-analysis/quality-rules/ca2253.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ helpviewer_keywords:
99
- LoggerMessageDefineAnalyzer
1010
- CA2253
1111
author: Youssef1313
12+
dev_langs:
13+
- CSharp
1214
---
1315
# CA2253: Named placeholders should not be numeric values
1416

@@ -34,6 +36,10 @@ Rename the numeric placeholder.
3436

3537
For usage examples, see the <xref:Microsoft.Extensions.Logging.LoggerExtensions.LogInformation%2A?displayProperty=nameWithType> method.
3638

39+
## Example
40+
41+
:::code language="csharp" source="snippets/csharp/all-rules/ca2253.cs" id="snippet1":::
42+
3743
## When to suppress errors
3844

3945
Do not suppress a warning from this rule.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace ca2253
4+
{
5+
//<snippet1>
6+
public class UserService
7+
{
8+
private readonly ILogger<UserService> _logger;
9+
10+
public UserService(ILogger<UserService> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
public void Add(string firstName, string lastName)
16+
{
17+
// This code violates the rule.
18+
_logger.LogInformation("Adding user with first name {0} and last name {1}", firstName, lastName);
19+
20+
// This code satisfies the rule.
21+
_logger.LogInformation("Adding user with first name {FirstName} and last name {LastName}", firstName, lastName);
22+
23+
// ...
24+
}
25+
}
26+
//</snippet1>
27+
}

0 commit comments

Comments
 (0)