multi-source.md 1.5 KB

Multi-Source Examples

Combining multiple configuration sources with level-based priority.

Local Files

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production";

var cfg = new CfgBuilder()
    .AddJson("config.json", level: 0, writeable: false)
    .AddJson($"config.{env}.json", level: 1, writeable: false, optional: true)
    .AddJson("config.local.json", level: 2, writeable: true, optional: true)
    .Build();

With Remote Sources

var cfg = new CfgBuilder()
    // Local base configuration
    .AddJson("config.json", level: 0, writeable: false)

    // Remote configuration center
    .AddConsul(options =>
    {
        options.Address = "http://consul:8500";
        options.KeyPrefix = "myapp/config/";
    }, level: 10, writeable: true, reloadOnChange: true)

    // Secrets from Vault
    .AddVault(options =>
    {
        options.Address = "http://vault:8200";
        options.SecretPath = "secret/myapp";
    }, level: 15)

    // Environment variable overrides
    .AddEnvironmentVariables(level: 20, prefix: "MYAPP_")
    .Build();

Mixed Formats

var cfg = new CfgBuilder()
    .AddJson("config.json", level: 0, writeable: false)
    .AddYaml("config.yaml", level: 1, writeable: false, optional: true)
    .AddToml("config.toml", level: 2, writeable: false, optional: true)
    .Build();

Next Steps