-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Closed
Labels
Description
The last code sample advises to use _logger.IsEnabled(LogLevel.Information) to avoid costly computations when logging is not enabled for the desired level.
The issue here is that now there are, at least, 2 places where the logging level needs to be coded.
A better advice would be to add an XXXIsEnabled method to the LogMessages class:
using Microsoft.Extensions.Logging;
namespace Logging.LibraryAuthors;
internal static partial class LogMessages
{
[LoggerMessage(
Message = "Sold {quantity} of {description}",
Level = LogLevel.Information,
SkipEnabledCheck = true)]
internal static partial void LogProductSaleDetails(
this ILogger logger,
int quantity,
string description);
internal static bool IsLogProductSaleDetailsEnabled(
this ILogger logger) => logger.IsEnabled(LogLevel.Information);
}Better yet would be for the code generator to generate that method.
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
- ID: 9cb767be-972d-9086-a759-d051fb11d555
- Version Independent ID: 3c6a20c4-b69f-8a21-3603-16d18a257ff4
- Content: Logging guidance for .NET library authors - .NET
- Content Source: docs/core/extensions/logging-library-authors.md
- Product: dotnet-fundamentals
- GitHub Login: @IEvangelist
- Microsoft Alias: dapine