A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1848 below:

CA1848: Use the LoggerMessage delegates (code analysis) - .NET

Property Value Rule ID CA1848 Title Use the LoggerMessage delegates Category Performance Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 No Cause

Use of logger extension methods, such as LogInformation and LogDebug.

Rule description

For high-performance logging scenarios, use the LoggerMessage pattern instead of Logger<T> extension methods.

How to fix violations

Use LoggerMessageAttribute to fix violations of this rule. (Or, if you're using .NET 5 or earlier, use the LoggerMessage class.)

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.

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();
}

LoggerMessage provides the following performance advantages over Logger<T> extension methods:

When to suppress warnings

Do not suppress a warning from this rule.

See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4