docs icon indicating copy to clipboard operation
docs copied to clipboard

Add example fix to CA1848

Open SemaphoreSlim1 opened this issue 6 months ago • 2 comments

Type of issue

Typo

Description

A few things would make this article better.

Rule Description:

Change the link under the rule description to point to the high performance logging article

Instead of:

For high-performance logging scenarios, use the LoggerMessage pattern.

Use this:

For high-performance logging scenarios, use the LoggerMessage pattern.

How to fix violations

add the following example code:

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

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

The rule is fixed by using the LoggerMessage pattern

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 = "We did something!")]
   private partial void Log_DidSomething();
}

Page URL

https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1848

Content source URL

https://github.com/dotnet/docs/blob/main/docs/fundamentals/code-analysis/quality-rules/ca1848.md

Document Version Independent Id

c3a759df-f039-53ca-46d7-4c7621c1491e

Platform Id

cc050d77-2a24-d521-77d1-e1283065f592

Article author

@Youssef1313

Metadata

  • ID: 963ffcee-06de-68ab-d920-f92528f1cded
  • PlatformId: cc050d77-2a24-d521-77d1-e1283065f592
  • Service: dotnet-fundamentals

Related Issues

SemaphoreSlim1 avatar May 16 '25 19:05 SemaphoreSlim1

An example would be good to have there, let me add it. CC: @BillWagner

BartoszKlonowski avatar May 29 '25 14:05 BartoszKlonowski

Hey @SemaphoreSlim1! I can't reproduce the CA1848 on my side locally, even when having all warnings switched on and warnings as errors. Can you share some a repo or .csproj file that I can use a reference to check?

BartoszKlonowski avatar Jun 11 '25 00:06 BartoszKlonowski