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/ca1848.md
+44-5Lines changed: 44 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: "CA1848: Use the LoggerMessage delegates (code analysis)"
3
3
description: "Learn about code analysis rule CA1848: Use the LoggerMessage delegates"
4
-
ms.date: 01/19/2022
4
+
ms.date: 08/04/2025
5
5
f1_keywords:
6
6
- "LoggerMessageDefineAnalyzer"
7
7
- "CA1848"
@@ -26,13 +26,51 @@ Use of [logger extension methods](xref:Microsoft.Extensions.Logging.LoggerExtens
26
26
27
27
## Rule description
28
28
29
-
For high-performance logging scenarios, use the <xref:Microsoft.Extensions.Logging.LoggerMessage> pattern.
29
+
For high-performance logging scenarios, use the [LoggerMessage](../../../core/extensions/logger-message-generator.md) pattern instead of <xref:Microsoft.Extensions.Logging.Logger`1> extension methods.
30
30
31
31
## How to fix violations
32
32
33
-
Use `LoggerMessage` to fix violations of this rule.
33
+
Use <xref:Microsoft.Extensions.Logging.LoggerMessageAttribute> to fix violations of this rule. (Or, if you're using .NET 5 or earlier, use the <xref:Microsoft.Extensions.Logging.LoggerMessage> class.)
34
34
35
-
<xref:Microsoft.Extensions.Logging.LoggerMessage> provides the following performance advantages over Logger extension methods:
<xref:Microsoft.Extensions.Logging.LoggerMessage> provides the following performance advantages over <xref:Microsoft.Extensions.Logging.Logger`1> extension methods:
36
74
37
75
- Logger extension methods require "boxing" (converting) value types, such as `int`, into `object`. The <xref:Microsoft.Extensions.Logging.LoggerMessage> pattern avoids boxing by using static <xref:System.Action> fields and extension methods with strongly typed parameters.
38
76
- Logger extension methods must parse the message template (named format string) every time a log message is written. <xref:Microsoft.Extensions.Logging.LoggerMessage> only requires parsing a template once when the message is defined.
@@ -43,5 +81,6 @@ Do not suppress a warning from this rule.
43
81
44
82
## See also
45
83
46
-
-[High-performance logging with LoggerMessage in ASP.NET Core](/aspnet/core/fundamentals/logging/loggermessage)
0 commit comments