All rules
CA2254Usage Enabled by default: As suggestion

Template should be a static expression

Template should be a static expression

Microsoft docs

Description

When performing logging, it's desirable to preserve the structure of the log (including placeholder names) along with the placeholder values. Preserving this information allows for better observability and search in log aggregation and monitoring software.

Preferred:

Not preferred:

The logging message template should not vary between calls.

Cause

A message template passed to a logger API is not constant. This occurs when the template passed uses either string concatenation or interpolation. Instead, the template should be a constant value that represents the log message in _message template format_. For example: "User {User} logged in from {Address}". For more information, see Log message template formatting.

How to fix violations

Update the message template to be a constant expression. If you're using values directly in the template, refactor the template to use named placeholders instead.

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

Example

logger.LogWarning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);

When to suppress

It's safe to suppress a warning from this rule if your use case doesn't require structured logging. It's also safe to suppress this rule if your log message template is defined in a resource file.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0