Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/fundamentals/code-analysis/quality-rules/ca2017.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
---
title: "CA2017: Parameter count mismatch (code analysis)"
description: "Learn about code analysis rule CA2017: Parameter count mismatch"
ms.date: 01/19/2022
ms.date: 09/24/2025
ai-usage: ai-assisted
f1_keywords:
- "LoggerMessageDefineAnalyzer"
- "CA2017"
helpviewer_keywords:
- "LoggerMessageDefineAnalyzer"
- "CA2017"
author: Youssef1313
dev_langs:
- CSharp
- VB
---
# CA2017: Parameter count mismatch

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

Do not suppress a warning from this rule.

## Example

The following example shows methods that violate CA2017 and methods that satisfy the rule.

:::code language="csharp" source="snippets/csharp/all-rules/ca2017.cs" id="snippet1":::

:::code language="vb" source="snippets/vb/all-rules/ca2017-parameter-count-mismatch_1.vb":::

## See also

- [Reliability rules](reliability-warnings.md)
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="System.Data.OleDb" Version="10.0.0-rc.1.25451.107" />
<PackageReference Include="System.Security.Permissions" Version="9.0.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.Extensions.Logging;

namespace ca2017
{
// <snippet1>
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);
}
}
// </snippet1>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="System.Data.OleDb" Version="10.0.0-rc.1.25451.107" />
<PackageReference Include="System.Data.SqlClient" Version="4.9.0" />
<PackageReference Include="System.Security.Permissions" Version="9.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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
Loading