Skip to content

Commit 371b4cd

Browse files
authored
Add example section to CA2017 documentation with C# and VB code snippets (#48743)
1 parent 000cff5 commit 371b4cd

File tree

5 files changed

+73
-1
lines changed

5 files changed

+73
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
---
22
title: "CA2017: Parameter count mismatch (code analysis)"
33
description: "Learn about code analysis rule CA2017: Parameter count mismatch"
4-
ms.date: 01/19/2022
4+
ms.date: 09/24/2025
5+
ai-usage: ai-assisted
56
f1_keywords:
67
- "LoggerMessageDefineAnalyzer"
78
- "CA2017"
89
helpviewer_keywords:
910
- "LoggerMessageDefineAnalyzer"
1011
- "CA2017"
1112
author: Youssef1313
13+
dev_langs:
14+
- CSharp
15+
- VB
1216
---
1317
# CA2017: Parameter count mismatch
1418

@@ -36,6 +40,14 @@ Match the number of placeholders in the template format with the number of passe
3640

3741
Do not suppress a warning from this rule.
3842

43+
## Example
44+
45+
The following example shows methods that violate CA2017 and methods that satisfy the rule.
46+
47+
:::code language="csharp" source="snippets/csharp/all-rules/ca2017.cs" id="snippet1":::
48+
49+
:::code language="vb" source="snippets/vb/all-rules/ca2017-parameter-count-mismatch_1.vb":::
50+
3951
## See also
4052

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
12+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
13+
<PackageReference Include="System.Data.OleDb" Version="10.0.0-rc.1.25451.107" />
1214
<PackageReference Include="System.Security.Permissions" Version="9.0.0" />
1315
</ItemGroup>
1416

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
30+
// </snippet1>
31+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
12+
<PackageReference Include="System.Data.OleDb" Version="10.0.0-rc.1.25451.107" />
1113
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
1214
<PackageReference Include="System.Security.Permissions" Version="9.0.0" />
1315
</ItemGroup>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Imports Microsoft.Extensions.Logging
2+
3+
Public Class LoggingExample
4+
5+
Private ReadOnly _logger As ILogger(Of LoggingExample)
6+
7+
Public Sub New(logger As ILogger(Of LoggingExample))
8+
_logger = logger
9+
End Sub
10+
11+
Public Sub ExampleMethod()
12+
Dim name As String = "Alice"
13+
Dim age As Integer = 30
14+
15+
' Violates CA2017: Too few arguments for placeholders.
16+
_logger.LogInformation("User {Name} is {Age} years old and lives in {City}", name, age)
17+
18+
' Violates CA2017: Too many arguments for placeholders.
19+
_logger.LogError("Error occurred: {Message}", "Something went wrong", "Extra argument")
20+
21+
' Correct usage: Matching number of placeholders and arguments.
22+
_logger.LogInformation("User {Name} is {Age} years old", name, age)
23+
End Sub
24+
25+
End Class

0 commit comments

Comments
 (0)