简体   繁体   English

Azure 应用配置重新加载失败时如何记录错误

[英]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.

相关问题 通过 ARM 模板安装时,琐碎的 Azure 逻辑应用程序失败 - Trivial Azure Logic App fails when installed via ARM template Azure 应用程序配置抛出异常,并且当禁用的 Key Vault 机密作为 KV 参考时不加载配置 - Azure app configuration throws exception & does not load the configuration when a disabled key vault secret is present as KV reference 如何将连接字符串存储在 Azure Vault 和 map 中返回到 Azure Web App 中的配置设置? - How to store connection string in Azure Vault and map it back to the configuration settings in Azure Web App? 如何从 Azure 应用程序配置中读取连接字符串? - How to read connection string from Azure app configuration? 如何将 JSON 从 Azure 应用程序配置解析为强类型 object - How to parse JSON from Azure App Configuration into an strongly typed object 如何登录Azure中的容器? - How to log into container in Azure? HTTP 在Azure上部署应用程序时出现413错误 - HTTP 413 Error When App is Deployed on Azure 刷新Azure 基于Label的App配置 - Refreshing Azure App Configuration Based on Label Key和Label对Azure App配置的限制 - Limitations of Key and Label for Azure App Configuration 无法更新 Azure App 中的配置设置 - Unable to Update Configuration Settings in Azure App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM