Skip to content

[Question] How to log an error when Azure App Configuration reload fails #713

Description

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

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NET Config ProviderIssues related to the AppConfig .NET Core configuration provider.question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions