Please see the following code which makes use of compile-time logging source generation:
public static partial class LoggerExtensions
{
[LoggerMessage(
EventId = 1953,
EventName = nameof(Processed),
Level = LogLevel.Debug,
Message = "Processed {Count} records.",
SkipEnabledCheck = true)]
public static partial void Processed(this ILogger logger, int count);
}
public class MyClass
{
private readonly ILogger _logger;
public MyClass(ILogger<MyClass> logger) => _logger = logger;
public void MyMethod()
{
_logger.Processed(42);
}
}
This is the related appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
}
}
As you can see, the log message Processed shall only be logged if the Debug log level is enabled - which works perfectly fine.
However, I'm confused about why this actually works from my understanding, the source generator converts the logging code to:
public static void Processed(this ILogger logger, int count)
{
if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("Processed {Count} records.", count);
}
}
By setting SkipEnabledCheck = true, this should become:
public static void Processed(this ILogger logger, int count)
{
logger.LogDebug("Processed {Count} records.", count);
}
However, if my assumption would be correct, the log statement should also appear when the minimum log level is set to Information rather than Debug because the necessary check is missing. But this is not the case, it gets only logged when setting the log level within appsettings.json to Debug.
What am I missing? ♂️
Thx!