Muokkaa

Jaa


CA1848: Use the LoggerMessage delegates

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.

How to fix violations

Use LoggerMessage to fix violations of this rule.

LoggerMessage provides the following performance advantages over Logger extension methods:

  • Logger extension methods require "boxing" (converting) value types, such as int, into object. The LoggerMessage pattern avoids boxing by using static 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. LoggerMessage only requires parsing a template once when the message is defined.

When to suppress warnings

Do not suppress a warning from this rule.

See also