From d1dbdf728f239b60a3189248352bc514249c300c Mon Sep 17 00:00:00 2001 From: David Pine Date: Tue, 22 Jun 2021 09:17:34 -0500 Subject: [PATCH 1/2] Use a Func to get the current config value --- docs/core/extensions/custom-logging-provider.md | 6 ++++-- .../console-custom-logging/ColorConsoleLogger.cs | 13 +++++++------ .../ColorConsoleLoggerProvider.cs | 4 +++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/docs/core/extensions/custom-logging-provider.md b/docs/core/extensions/custom-logging-provider.md index e1ee7e5bd8ee8..26427dd59708c 100644 --- a/docs/core/extensions/custom-logging-provider.md +++ b/docs/core/extensions/custom-logging-provider.md @@ -3,7 +3,7 @@ title: Implement a custom logging provider in .NET description: Learn how to implement a custom logging provider in your .NET applications. author: IEvangelist ms.author: dapine -ms.date: 05/06/2021 +ms.date: 06/22/2021 ms.topic: how-to --- @@ -28,10 +28,12 @@ The `ILogger` implementation category name is typically the logging source. For The preceding code: - Creates a logger instance per category name. -- Checks `_config.LogLevels.ContainsKey(logLevel)` in `IsEnabled`, so each `logLevel` has a unique logger. Loggers should also be enabled for all higher log levels: +- Checks `_getCurrentConfig().LogLevels.ContainsKey(logLevel)` in `IsEnabled`, so each `logLevel` has a unique logger. Loggers should also be enabled for all higher log levels: :::code language="csharp" source="snippets/configuration/console-custom-logging/ColorConsoleLogger.cs" range="16-17"::: +The logger is instantiated with the `name` and a `Func` which will return the current config — this handles updates to the config values as monitored through the callback. + ## Custom logger provider The `ILoggerProvider` object is responsible for creating logger instances. Maybe it is not needed to create a logger instance per category, but this makes sense for some loggers, like NLog or log4net. Doing this you are also able to choose different logging output targets per category if needed: diff --git a/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLogger.cs b/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLogger.cs index 4f9566b769681..74cbf4ccc895c 100644 --- a/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLogger.cs +++ b/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLogger.cs @@ -4,17 +4,17 @@ public class ColorConsoleLogger : ILogger { private readonly string _name; - private readonly ColorConsoleLoggerConfiguration _config; + private readonly Func _getCurrentConfig; public ColorConsoleLogger( string name, - ColorConsoleLoggerConfiguration config) => - (_name, _config) = (name, config); + Func getCurrentConfig) => + (_name, _getCurrentConfig) = (name, getCurrentConfig); public IDisposable BeginScope(TState state) => default; public bool IsEnabled(LogLevel logLevel) => - _config.LogLevels.ContainsKey(logLevel); + _getCurrentConfig().LogLevels.ContainsKey(logLevel); public void Log( LogLevel logLevel, @@ -28,11 +28,12 @@ public void Log( return; } - if (_config.EventId == 0 || _config.EventId == eventId.Id) + ColorConsoleLoggerConfiguration config = _getCurrentConfig(); + if (config.EventId == 0 || config.EventId == eventId.Id) { ConsoleColor originalColor = Console.ForegroundColor; - Console.ForegroundColor = _config.LogLevels[logLevel]; + Console.ForegroundColor = config.LogLevels[logLevel]; Console.WriteLine($"[{eventId.Id,2}: {logLevel,-12}]"); Console.ForegroundColor = originalColor; diff --git a/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLoggerProvider.cs b/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLoggerProvider.cs index 859aa89c98661..ab61e1f01df84 100644 --- a/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLoggerProvider.cs +++ b/docs/core/extensions/snippets/configuration/console-custom-logging/ColorConsoleLoggerProvider.cs @@ -17,7 +17,9 @@ public ColorConsoleLoggerProvider( } public ILogger CreateLogger(string categoryName) => - _loggers.GetOrAdd(categoryName, name => new ColorConsoleLogger(name, _currentConfig)); + _loggers.GetOrAdd(categoryName, name => new ColorConsoleLogger(name, GetCurrentConfig)); + + private ColorConsoleLoggerConfiguration GetCurrentConfig() => _currentConfig; public void Dispose() { From e5cfd9003cb4cf1232f21fa93df3e91877d8e579 Mon Sep 17 00:00:00 2001 From: David Pine Date: Tue, 22 Jun 2021 10:34:12 -0500 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Cam Soper --- docs/core/extensions/custom-logging-provider.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/core/extensions/custom-logging-provider.md b/docs/core/extensions/custom-logging-provider.md index 26427dd59708c..2858a7bf34d0a 100644 --- a/docs/core/extensions/custom-logging-provider.md +++ b/docs/core/extensions/custom-logging-provider.md @@ -36,7 +36,7 @@ The logger is instantiated with the `name` and a `Func