Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 24, 2025

Fixes #[issue-number] by adding comprehensive examples to the CA2017 code analysis rule documentation.

Changes Made

Added an Example section to the CA2017 documentation (ca2017.md) that demonstrates both violations and correct usage of logging message templates with parameter count mismatches.

Code Snippets Added

C# Example (snippets/csharp/all-rules/ca2017.cs):

// 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);

VB Example (snippets/vb/all-rules/ca2017-parameter-count-mismatch_1.vb):

' Violates CA2017: Too few arguments for placeholders
_logger.LogInformation("User {Name} is {Age} years old and lives in {City}", name, age)

' Correct usage: Matching number of placeholders and arguments
_logger.LogInformation("User {Name} is {Age} years old", name, age)

Project Updates

  • Added Microsoft.Extensions.Logging package dependency to both C# and VB snippet projects
  • Updated target framework to .NET 8.0 for compatibility
  • Added AI usage disclosure to documentation frontmatter per Microsoft guidelines

Verification

The code snippets compile successfully and correctly trigger CA2017 warnings on the violating lines, demonstrating the rule's behavior in real scenarios. This provides users with clear, actionable examples of when the rule fires and how to resolve violations.

The examples cover common scenarios developers encounter when working with structured logging in .NET applications, making the documentation more helpful and practical.


Additional instructions:

You can find info about the analyzer here: https://github.com/dotnet/sdk/blob/ec550ce1fd98e07f26af63f986c2506a410ecac2/src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/LoggerMessageDefineAnalyzer.cs#L24

Fixes #47980


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.


Internal previews

📄 File 🔗 Preview link
docs/fundamentals/code-analysis/quality-rules/ca2017.md CA2017: Parameter count mismatch

@Copilot Copilot AI changed the title [WIP] [UUF] Example would be helpful Add example section to CA2017 documentation with C# and VB code snippets Sep 24, 2025
@Copilot Copilot AI requested a review from gewarren September 24, 2025 22:04
Copilot finished work on behalf of gewarren September 24, 2025 22:04
@gewarren gewarren marked this pull request as ready for review September 25, 2025 00:40
@gewarren gewarren requested a review from a team as a code owner September 25, 2025 00:40
@gewarren gewarren merged commit 371b4cd into main Sep 25, 2025
10 checks passed
@gewarren gewarren deleted the copilot/fix-53d6069d-978c-4a75-a4b0-1e0de1212989 branch September 25, 2025 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[UUF] Example would be helpful
3 participants