LoggerIsEnabled.Fody icon indicating copy to clipboard operation
LoggerIsEnabled.Fody copied to clipboard

Add-in for Fody, ads ILogger.IsEnabled check to your logging statements

Build status

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.

Usage

See also Fody usage.

NuGet installation

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 to FodyWeavers.xml

Add <LoggerIsEnabled/> to FodyWeavers.xml

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <LoggerIsEnabled/>
</Weavers>

Your Code

For all scenarios see AssemblyToProcess

public class Example
{
    private readonly ILogger _logger;

    public Example(ILogger logger)
    {
        _logger = logger;
    }

    public void MethodWithLogging()
    {
        _logger.LogTrace("message");
    }
}

What gets compiled


public class Example
{
    private readonly ILogger _logger;

    public Example(ILogger logger)
    {
        _logger = logger;
    }

    public void MethodWithLogging()
    {
        if (_logger.IsEnabled(LogLevel.Trace))
        {
            _logger.LogTrace("message");
        }
    }
}