How can I reuse a structured logging message without violating CA2254?
01:32 01 Sep 2026

Microsoft quality rule CA2254 complains that log message template should be a static expression.

For example, the rule recommends:

// DO NOT DO THIS
var firstName = "Lorenz";
var lastName = "Otto";

// String interpolation also loses the association between placeholder names and their values.
logger.LogWarning($"Person {firstName} {lastName} encountered an issue");

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

However, in our case, we want to reuse the error message for both logging and publishing it to an external service:

var firstName = "Lorenz";
var lastName = "Otto";
ver errorMessage = $"Person {firstName} {lastName} encountered an issue";

logger.LogWarning(errorMessage);
// logger.LogWarning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);
eventBus.Publish(new Payload(errorMessage));

What is the recommended way to keep the lint happy while avoiding duplication of the message? Ideally I would like to define the message only once while still preserving structured logging.

best-practices c#