File tree Expand file tree Collapse file tree 5 files changed +86
-4
lines changed
docs/fundamentals/code-analysis/quality-rules Expand file tree Collapse file tree 5 files changed +86
-4
lines changed Original file line number Diff line number Diff line change @@ -36,6 +36,14 @@ Match the number of placeholders in the template format with the number of passe
36
36
37
37
Do not suppress a warning from this rule.
38
38
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
+
39
47
## See also
40
48
41
49
- [ Reliability rules] ( reliability-warnings.md )
Original file line number Diff line number Diff line change 2
2
3
3
<PropertyGroup >
4
4
<OutputType >Exe</OutputType >
5
- <TargetFramework >net9 .0</TargetFramework >
5
+ <TargetFramework >net8 .0</TargetFramework >
6
6
<Nullable >enable</Nullable >
7
7
<RootNamespace >all_rules</RootNamespace >
8
8
</PropertyGroup >
9
9
10
10
<ItemGroup >
11
+ <PackageReference Include =" Microsoft.Extensions.Logging" Version =" 8.0.0" />
11
12
<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" />
13
14
</ItemGroup >
14
15
15
16
<ItemGroup >
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
+ // 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
+ }
Original file line number Diff line number Diff line change 3
3
<PropertyGroup >
4
4
<OutputType >Exe</OutputType >
5
5
<RootNamespace >all_rules</RootNamespace >
6
- <TargetFramework >net9 .0</TargetFramework >
6
+ <TargetFramework >net8 .0</TargetFramework >
7
7
<NoWarn >$(NoWarn);SYSLIB0050</NoWarn >
8
8
</PropertyGroup >
9
9
10
10
<ItemGroup >
11
+ <PackageReference Include =" Microsoft.Extensions.Logging" Version =" 8.0.0" />
11
12
<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" />
13
14
</ItemGroup >
14
15
15
16
</Project >
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments