Skip to content

IOptionsMonitor in example doesn't update config in existing Loggers #24533

@hannahchan

Description

@hannahchan

The current example for ColorConsoleLoggerProvider which usesIOptionsMonitor<ColorConsoleLoggerConfiguration> doesn't update the config for existing loggers that have already been instantiated and stored in _loggers. When the configuration changes, these loggers continue to reference the old ColorConsoleLoggerConfiguration instance and only the new instances take on the configuration change.

One suggestion to fix this is to update the following line in the constructor from;

_onChangeToken = config.OnChange(updatedConfig => _currentConfig = updatedConfig);

to;

_onChangeToken = config.OnChange(updatedConfig =>
{
    _currentConfig = updatedConfig;
    _loggers.Clear();
});

Other than that, I'm wondering if there are best practices that other developers like me should know about when implementing a custom logging provider. For example, how to make an async HTTP request to a logging backend.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions