[英]How to log an error when Azure App Configuration reload fails
I'm using Azure App Configuration with the dynamic configuration using poll model enabled, as described here .我正在使用 Azure 应用程序配置,并启用了使用轮询 model 的动态配置,如此处所述。 My goal is to log an error message when the configuration reload fails for any reason, like an incorrect key-vault reference.
我的目标是在由于任何原因重新加载配置失败时记录一条错误消息,例如不正确的密钥库引用。 Here's the method which adds Azure App Configuration as the configuration source.
这是添加 Azure App Configuration 作为配置源的方法。
public static IConfigurationBuilder AddAppConfiguration(IConfigurationBuilder configurationBuilder, IEnumerable<string> filters)
{
if (!configurationBuilder.Sources.Any())
{
configurationBuilder.AddDefaultConfigurationSources();
}
var configuration = configurationBuilder.Build();
var appConfiguration = configuration.GetSection(AppConfiguration.ConfigurationKey)
.Get<AppConfiguration>();
if (appConfiguration?.IsEnabled == true)
{
if (appConfiguration.IsRedundancyEnabled)
configurationBuilder.AddAzureAppConfiguration(
options =>
{
ConfigureAppConfiguration(options, appConfiguration, appConfiguration.UrlSecondary, filters, appConfiguration.Label);
}, true);
configurationBuilder.AddAzureAppConfiguration(
options => { ConfigureAppConfiguration(options, appConfiguration, appConfiguration.Url, filters, appConfiguration.Label); },
true);
}
return configurationBuilder;
}
Please note that the optional
parameter in AddAzureAppConfiguration
is set to true
.请注意,
AddAzureAppConfiguration
中的optional
参数设置为true
。 This means that the application won't crash if the configuration refresh fails and will just keep running with the old settings, I'd like to keep that behavior.这意味着如果配置刷新失败,应用程序不会崩溃,只会继续使用旧设置运行,我想保持这种行为。
I've tried to use the ChangeToken to log an error if the configuration hasn't changed like this:如果配置没有像这样更改,我尝试使用ChangeToken记录错误:
configuration.GetReloadToken().RegisterChangeCallback(state =>
{
var reloadToken = state as IChangeToken;
if (reloadToken.HasChanged)
{
logger.Information("Configuration refresh successful");
}
else
{
logger.Error("Configuration refresh failed");
}
RegisterCallback(logger, configuration);
}, configuration.GetReloadToken());
But the callback only actually gets called if the configuration did in fact change, at least I didn't manage to get it to fail and log the error.但是回调只有在配置确实发生变化时才会被调用,至少我没有设法让它失败并记录错误。
As explained here , the default ILoggerFactory will be added automatically when services.AddAzureAppConfiguration()
is invoked in your ConfigureServices()
method through DI.如此处所述,当通过 DI 在
ConfigureServices()
方法中调用services.AddAzureAppConfiguration()
时,将自动添加默认 ILoggerFactory。 I was not seeing logs because the Microsoft minimum log level was set to Error , while the AzureAppConfigurationProvider logs Warning if the TryRefreshAsync()
method fails.我没有看到日志,因为 Microsoft 最低日志级别设置为Error ,而如果
TryRefreshAsync()
方法失败,AzureAppConfigurationProvider 会记录警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.