Use of logger extension methods, such as LogInformation and LogDebug.
Rule descriptionFor high-performance logging scenarios, use the LoggerMessage pattern instead of Logger<T> extension methods.
How to fix violationsUse 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:
int
, into object
. The LoggerMessage pattern avoids boxing by using static Action fields and extension methods with strongly typed parameters.Do not suppress a warning from this rule.
See alsoRetroSearch 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