C#idiomatic C#
ca1727fundamentals/code-analysis/quality-rules

Code analysis

CA1727: Use PascalCase for named placeholders

Learn about code analysis rule CA1727: Use PascalCase for named placeholders

CA1727 Naming enabled by default in .NET 10: No
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

A named placeholder used with Microsoft.Extensions.Logging.ILogger is not PascalCase.

Rule description

A named placeholder used with Microsoft.Extensions.Logging.ILogger should be PascalCase, a naming convention where the first letter of each compound word in a name is capitalized. This naming convention is recommended for structured logging, where each named placeholder is used as a property name in the structured data.

How to fix violations

Use PascalCase for named placeholders. For example, change {firstName} to {FirstName}.

Example

sample · csharpdocs/fundamentals/code-analysis/quality-rules/snippets/csharp/all-rules/ca1727.cssnippet1

csharp
using Microsoft.Extensions.Logging;

namespace ca1727
{
    //<snippet1>
    public class UserService
    {
        private readonly ILogger<UserService> _logger;

        public UserService(ILogger<UserService> logger)
        {
            _logger = logger;
        }

        public void Create(string firstName, string lastName)
        {
            // This code violates the rule.
            _logger.LogInformation("Creating user {firstName} {lastName}", firstName, lastName);

            // This code satisfies the rule.
            _logger.LogInformation("Creating user {FirstName} {LastName}", firstName, lastName);
        }
    }
    //</snippet1>
}

When to suppress warnings

It is safe to suppress a warning from this rule.

Suppress a warning

If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.

csharp
#pragma warning disable CA1727
// The code that's violating the rule is on this line.
#pragma warning restore CA1727

To disable the rule for a file, folder, or project, set its severity to none in the configuration file.

ini
[*.{cs,vb}]
dotnet_diagnostic.CA1727.severity = none

For more information, see How to suppress code analysis warnings.