You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/fundamentals/code-analysis/quality-rules/ca2254.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ var lastName = "Otto";
36
36
37
37
// This tells the logger that there are FirstName and LastName properties
38
38
// on the log message, and correlates them with the argument values.
39
-
logger.Warning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);
39
+
logger.LogWarning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);
40
40
```
41
41
42
42
Not preferred:
@@ -48,10 +48,10 @@ var firstName = "Lorenz";
48
48
varlastName="Otto";
49
49
50
50
// Here, the log template itself is changing, and the association between named placeholders and their values is lost.
51
-
logger.Warning("Person "+firstName+""+lastName+" encountered an issue");
51
+
logger.LogWarning("Person "+firstName+""+lastName+" encountered an issue");
52
52
53
53
// String interpolation also loses the association between placeholder names and their values.
54
-
logger.Warning($"Person {firstName} {lastName} encountered an issue");
54
+
logger.LogWarning($"Person {firstName} {lastName} encountered an issue");
55
55
```
56
56
57
57
The logging message template should not vary between calls.
@@ -61,7 +61,7 @@ The logging message template should not vary between calls.
61
61
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.
62
62
63
63
```csharp
64
-
logger.Warning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);
64
+
logger.LogWarning("Person {FirstName} {LastName} encountered an issue", firstName, lastName);
65
65
```
66
66
67
67
For usage examples, see the <xref:Microsoft.Extensions.Logging.LoggerExtensions.LogInformation%2A?displayProperty=nameWithType> method.
0 commit comments