Skip to content

Commit 9f371b3

Browse files
Copilotgewarren
andcommitted
Add example section to CA2017 documentation with C# and VB snippets
Co-authored-by: gewarren <[email protected]>
1 parent 20fef81 commit 9f371b3

File tree

5 files changed

+86
-4
lines changed

5 files changed

+86
-4
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ Match the number of placeholders in the template format with the number of passe
3636

3737
Do not suppress a warning from this rule.
3838

39+
## Example
40+
41+
The following example shows methods that violate CA2017 and methods that satisfy the rule.
42+
43+
:::code language="csharp" source="snippets/csharp/all-rules/ca2017.cs" id="snippet1":::
44+
45+
:::code language="vb" source="snippets/vb/all-rules/ca2017-parameter-count-mismatch_1.vb":::
46+
3947
## See also
4048

4149
- [Reliability rules](reliability-warnings.md)

docs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/all-rules.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<Nullable>enable</Nullable>
77
<RootNamespace>all_rules</RootNamespace>
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
1112
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
12-
<PackageReference Include="System.Security.Permissions" Version="9.0.0" />
13+
<PackageReference Include="System.Security.Permissions" Version="8.0.0" />
1314
</ItemGroup>
1415

1516
<ItemGroup>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace ca2017
4+
{
5+
//<snippet1>
6+
public class LoggingExample
7+
{
8+
private readonly ILogger<LoggingExample> _logger;
9+
10+
public LoggingExample(ILogger<LoggingExample> logger)
11+
{
12+
_logger = logger;
13+
}
14+
15+
public void ExampleMethod()
16+
{
17+
string name = "Alice";
18+
int age = 30;
19+
20+
// Violates CA2017: Too few arguments for placeholders
21+
_logger.LogInformation("User {Name} is {Age} years old and lives in {City}", name, age);
22+
23+
// Violates CA2017: Too many arguments for placeholders
24+
_logger.LogError("Error occurred: {Message}", "Something went wrong", "Extra argument");
25+
26+
// Correct usage: Matching number of placeholders and arguments
27+
_logger.LogInformation("User {Name} is {Age} years old", name, age);
28+
29+
// Correct usage: No placeholders, no arguments
30+
_logger.LogInformation("Application started");
31+
32+
// Correct usage: Single placeholder, single argument
33+
_logger.LogWarning("Failed to process {FileName}", "document.txt");
34+
}
35+
}
36+
//</snippet1>
37+
}

docs/fundamentals/code-analysis/quality-rules/snippets/vb/all-rules/all-rules.vbproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<RootNamespace>all_rules</RootNamespace>
6-
<TargetFramework>net9.0</TargetFramework>
6+
<TargetFramework>net8.0</TargetFramework>
77
<NoWarn>$(NoWarn);SYSLIB0050</NoWarn>
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
1112
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
12-
<PackageReference Include="System.Security.Permissions" Version="9.0.0" />
13+
<PackageReference Include="System.Security.Permissions" Version="8.0.0" />
1314
</ItemGroup>
1415

1516
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Imports Microsoft.Extensions.Logging
2+
3+
Namespace ca2017
4+
5+
Public Class LoggingExample
6+
7+
Private ReadOnly _logger As ILogger(Of LoggingExample)
8+
9+
Public Sub New(logger As ILogger(Of LoggingExample))
10+
_logger = logger
11+
End Sub
12+
13+
Public Sub ExampleMethod()
14+
Dim name As String = "Alice"
15+
Dim age As Integer = 30
16+
17+
' Violates CA2017: Too few arguments for placeholders
18+
_logger.LogInformation("User {Name} is {Age} years old and lives in {City}", name, age)
19+
20+
' Violates CA2017: Too many arguments for placeholders
21+
_logger.LogError("Error occurred: {Message}", "Something went wrong", "Extra argument")
22+
23+
' Correct usage: Matching number of placeholders and arguments
24+
_logger.LogInformation("User {Name} is {Age} years old", name, age)
25+
26+
' Correct usage: No placeholders, no arguments
27+
_logger.LogInformation("Application started")
28+
29+
' Correct usage: Single placeholder, single argument
30+
_logger.LogWarning("Failed to process {FileName}", "document.txt")
31+
End Sub
32+
33+
End Class
34+
35+
End Namespace

0 commit comments

Comments
 (0)