Skip to content

Fix CA1848 documentation: Update link and add code examples #47579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
49 changes: 44 additions & 5 deletions docs/fundamentals/code-analysis/quality-rules/ca1848.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "CA1848: Use the LoggerMessage delegates (code analysis)"
description: "Learn about code analysis rule CA1848: Use the LoggerMessage delegates"
ms.date: 01/19/2022
ms.date: 08/04/2025
f1_keywords:
- "LoggerMessageDefineAnalyzer"
- "CA1848"
Expand All @@ -26,13 +26,51 @@ Use of [logger extension methods](xref:Microsoft.Extensions.Logging.LoggerExtens

## Rule description

For high-performance logging scenarios, use the <xref:Microsoft.Extensions.Logging.LoggerMessage> pattern.
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.

## How to fix violations

Use `LoggerMessage` to fix violations of this rule.
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.)

<xref:Microsoft.Extensions.Logging.LoggerMessage> provides the following performance advantages over Logger extension methods:
```csharp
public class SomethingDoer
{
private readonly ILogger _logger;
public SomethingDoer(ILogger<SomethingDoer> logger)
{
_logger = logger;
}

public void DoSomething()
{
// This call violates CA1848.
_logger.LogInformation("Did something!");
}
}
```

The following code fixes the violation.

```csharp
public partial class SomethingDoer
{
private readonly ILogger _logger;
public SomethingDoer(ILogger<SomethingDoer> logger)
{
_logger = logger;
}

public void DoSomething()
{
Log_DidSomething();
}

[LoggerMessage(Level = LogLevel.Information, Message = "Did something!")]
private partial void Log_DidSomething();
}
```

<xref:Microsoft.Extensions.Logging.LoggerMessage> provides the following performance advantages over <xref:Microsoft.Extensions.Logging.Logger`1> extension methods:

- 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.
- 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.
Expand All @@ -43,5 +81,6 @@ Do not suppress a warning from this rule.

## See also

- [High-performance logging with LoggerMessage in ASP.NET Core](/aspnet/core/fundamentals/logging/loggermessage)
- [Compile-time logging source generation](../../../core/extensions/logger-message-generator.md)
- [High-performance logging in .NET](../../../core/extensions/high-performance-logging.md)
- [Performance rules](performance-warnings.md)