Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/fundamentals/code-analysis/quality-rules/ca2254.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var lastName = "Otto";

// This tells the logger that there are FirstName and LastName properties
// on the log message, and correlates them with the argument values.
logger.Warning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);
logger.LogWarning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);
```

Not preferred:
Expand All @@ -48,10 +48,10 @@ var firstName = "Lorenz";
var lastName = "Otto";

// Here, the log template itself is changing, and the association between named placeholders and their values is lost.
logger.Warning("Person " + firstName + " " + lastName + " encountered an issue");
logger.LogWarning("Person " + firstName + " " + lastName + " encountered an issue");

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

The logging message template should not vary between calls.
Expand All @@ -61,7 +61,7 @@ The logging message template should not vary between calls.
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.

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

For usage examples, see the <xref:Microsoft.Extensions.Logging.LoggerExtensions.LogInformation%2A?displayProperty=nameWithType> method.
Expand Down
Loading