Skip to content

Commit 4d7602c

Browse files
Copilotgewarren
andcommitted
Fix CA1848 documentation: update link and add examples
- Update LoggerMessage link to point to high-performance logging article - Add before/after code examples showing CA1848 violation and fix - Examples demonstrate proper LoggerMessage pattern usage Co-authored-by: gewarren <[email protected]>
1 parent 244bead commit 4d7602c

File tree

1 file changed

+38
-1
lines changed
  • docs/fundamentals/code-analysis/quality-rules

1 file changed

+38
-1
lines changed

docs/fundamentals/code-analysis/quality-rules/ca1848.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,49 @@ Use of [logger extension methods](xref:Microsoft.Extensions.Logging.LoggerExtens
2626

2727
## Rule description
2828

29-
For high-performance logging scenarios, use the <xref:Microsoft.Extensions.Logging.LoggerMessage> pattern.
29+
For high-performance logging scenarios, use the [LoggerMessage](https://learn.microsoft.com/en-us/dotnet/core/extensions/high-performance-logging) pattern.
3030

3131
## How to fix violations
3232

3333
Use `LoggerMessage` to fix violations of this rule.
3434

35+
```csharp
36+
public class SomethingDoer
37+
{
38+
private readonly ILogger _logger;
39+
public SomethingDoer(ILogger<SomethingDoer> logger)
40+
{
41+
_logger = logger;
42+
}
43+
44+
public void DoSomething()
45+
{
46+
_logger.LogInformation("We did something!"); //This call violates CA1848
47+
}
48+
}
49+
```
50+
51+
The rule is fixed by using the [LoggerMessage pattern](https://learn.microsoft.com/en-us/dotnet/core/extensions/high-performance-logging):
52+
53+
```csharp
54+
public partial class SomethingDoer
55+
{
56+
private readonly ILogger _logger;
57+
public SomethingDoer(ILogger<SomethingDoer> logger)
58+
{
59+
_logger = logger;
60+
}
61+
62+
public void DoSomething()
63+
{
64+
Log_DidSomething();
65+
}
66+
67+
[LoggerMessage(Level = LogLevel.Information, Message = "We did something!")]
68+
private partial void Log_DidSomething();
69+
}
70+
```
71+
3572
<xref:Microsoft.Extensions.Logging.LoggerMessage> provides the following performance advantages over Logger extension methods:
3673

3774
- 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.

0 commit comments

Comments
 (0)