简体   繁体   中英

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 . 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.

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 . 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:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM