Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
| Property | Value |
|---|---|
| Rule ID | CA2017 |
| Title | Parameter count mismatch |
| Category | Reliability |
| Fix is breaking or non-breaking | Non-breaking |
| Enabled by default in .NET 9 | As warning |
Cause
The number of parameters supplied in the logging message template does not match the number of named placeholders.
Rule description
This rule flags logger calls that have an incorrect number of message arguments.
How to fix violations
Match the number of placeholders in the template format with the number of passed arguments.
When to suppress warnings
Do not suppress a warning from this rule.
Example
The following example shows methods that violate CA2017 and methods that satisfy the rule.
public class LoggingExample
{
private readonly ILogger<LoggingExample> _logger;
public LoggingExample(ILogger<LoggingExample> logger)
{
_logger = logger;
}
public void ExampleMethod()
{
string name = "Alice";
int age = 30;
// Violates CA2017: Too few arguments for placeholders.
_logger.LogInformation("User {Name} is {Age} years old and lives in {City}", name, age);
// Violates CA2017: Too many arguments for placeholders.
_logger.LogError("Error occurred: {Message}", "Something went wrong", "Extra argument");
// Correct usage: Matching number of placeholders and arguments.
_logger.LogInformation("User {Name} is {Age} years old", name, age);
}
}
Imports Microsoft.Extensions.Logging
Public Class LoggingExample
Private ReadOnly _logger As ILogger(Of LoggingExample)
Public Sub New(logger As ILogger(Of LoggingExample))
_logger = logger
End Sub
Public Sub ExampleMethod()
Dim name As String = "Alice"
Dim age As Integer = 30
' Violates CA2017: Too few arguments for placeholders.
_logger.LogInformation("User {Name} is {Age} years old and lives in {City}", name, age)
' Violates CA2017: Too many arguments for placeholders.
_logger.LogError("Error occurred: {Message}", "Something went wrong", "Extra argument")
' Correct usage: Matching number of placeholders and arguments.
_logger.LogInformation("User {Name} is {Age} years old", name, age)
End Sub
End Class