Skip to content

Conversation

Meir017
Copy link

@Meir017 Meir017 commented Sep 11, 2025

@Copilot Copilot AI review requested due to automatic review settings September 11, 2025 19:35
@Meir017 Meir017 requested a review from a team as a code owner September 11, 2025 19:35
Copy link
Contributor

This PR is targeting main, which is now for .NET 11-facing work. If you intended to target .NET 10, either retarget this PR to release/10.0.1xx or make sure you backport the change to release/10.0.1xx after merging. See #50394 for more details.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds analysis for camel-cased format arguments in LoggerMessageAttribute to the CA1727 rule, extending its current coverage beyond just LoggerMessage.Define calls to also include modern source generator-based logging methods.

  • Adds symbol analysis for methods decorated with LoggerMessageAttribute to detect camel-cased format placeholders
  • Refactors existing analysis logic into a shared core method to support both operation and symbol-based analysis contexts
  • Includes comprehensive test coverage for both positional and named LoggerMessageAttribute constructors

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
LoggerMessageDefineAnalyzer.cs Adds symbol analysis registration and core refactoring to analyze LoggerMessageAttribute message templates
LoggerMessageDefineTests.cs Adds test cases for CA1727 rule validation with LoggerMessageAttribute scenarios
Comments suppressed due to low confidence (1)

src/Microsoft.CodeAnalysis.NetAnalyzers/src/Microsoft.CodeAnalysis.NetAnalyzers/Microsoft.NetCore.Analyzers/Runtime/LoggerMessageDefineAnalyzer.cs:1

  • The catch block only handles ArgumentException, but LogValuesFormatter constructor may throw other exceptions like IndexOutOfRangeException or FormatException for malformed templates. Consider catching a broader exception type or multiple specific exceptions to ensure all invalid template scenarios are handled properly.
// Copyright (c) Microsoft.  All Rights Reserved.  Licensed under the MIT license.  See License.txt in the project root for license information.

Comment on lines +267 to +278
AnalyzeMessageTemplateCore(text,
onInvalidTemplate: () => context.ReportDiagnostic(formatExpression.CreateDiagnostic(CA2023Rule)),
onNumericPlaceholder: () => context.ReportDiagnostic(formatExpression.CreateDiagnostic(CA2253Rule)),
onCamelCasePlaceholder: () => context.ReportDiagnostic(formatExpression.CreateDiagnostic(CA1727Rule)),
onParameterCountMismatch: (expectedCount) =>
{
var argsPassedDirectly = argsIsArray && paramsCount == 1;
if (!argsPassedDirectly && paramsCount != expectedCount)
{
context.ReportDiagnostic(formatExpression.CreateDiagnostic(CA2017Rule));
}
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each call like this will allocate a closure and several delegates, which could harm performance.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I'll see how I could make these static lambdas, that should fix the issue, right?

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.

CA1727 is not reported for LoggerMessageAttribute
2 participants