File tree Expand file tree Collapse file tree 5 files changed +73
-1
lines changed
docs/fundamentals/code-analysis/quality-rules Expand file tree Collapse file tree 5 files changed +73
-1
lines changed Original file line number Diff line number Diff line change 11---
22title : " CA2017: Parameter count mismatch (code analysis)"
33description : " 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
56f1_keywords :
67 - " LoggerMessageDefineAnalyzer"
78 - " CA2017"
89helpviewer_keywords :
910 - " LoggerMessageDefineAnalyzer"
1011 - " CA2017"
1112author : 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
3741Do 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 )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments