This is an add-in for Fody
For the Microsoft.Extensions.Logging.ILogger interface, this will add the ILogger.IsEnabled(LogLevel) check around the logging statement. To reduce the boilerplate code, but still have the performance benefit when a certain LogLevel is turned off.
See also Fody usage.
Install the LoggerIsEnabled.Fody NuGet package and update the Fody NuGet package:
PM> Install-Package LoggerIsEnabled.Fody
PM> Update-Package Fody
The Update-Package Fody
is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.
Add <LoggerIsEnabled/>
to FodyWeavers.xml
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<LoggerIsEnabled/>
</Weavers>
For all scenarios see AssemblyToProcess
public class Example
{
private readonly ILogger _logger;
public Example(ILogger logger)
{
_logger = logger;
}
public void MethodWithLogging()
{
_logger.LogTrace("message");
}
}
public class Example
{
private readonly ILogger _logger;
public Example(ILogger logger)
{
_logger = logger;
}
public void MethodWithLogging()
{
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("message");
}
}
}