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

Code analysis

CA2253: Named placeholders should not be numeric values

Learn about code analysis rule CA2253: Named placeholders should not be numeric values

CA2253 Usage enabled by default in .NET 10: As suggestion
Fix is breaking or non-breaking Non-breakingApplicable languages C# and Visual Basic

Cause

A message placeholder consists of numeric characters only.

Rule description

Named placeholders in the logging message template should not be composed of only numeric characters.

How to fix violations

Rename the numeric placeholder.

For usage examples, see the Microsoft.Extensions.Logging.LoggerExtensions.LogInformation method.

Example

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

csharp
using Microsoft.Extensions.Logging;

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

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

        public void Add(string firstName, string lastName)
        {
            // This code violates the rule.
            _logger.LogInformation("Adding user with first name {0} and last name {1}", firstName, lastName);

            // This code satisfies the rule.
            _logger.LogInformation("Adding user with first name {FirstName} and last name {LastName}", firstName, lastName);

            // ...
        }
    }
    //</snippet1>
}

When to suppress errors

Do not suppress a warning from this rule.

See also