Skip to content

bugproof/Tomlyn.Extensions.Configuration

Repository files navigation

Tomlyn.Extensions.Configuration NuGet

TomlConfigurationProvider using Tomlyn

Mostly based on Microsoft.Extensions.Configuration.Json

Usage

// ASP.NET Core
public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureAppConfiguration((hostingContext, config) =>
        {
            config.Sources.Clear(); // CreateDefaultBuilder adds default configuration sources like appsettings.json. Here we can remove them

            var env = hostingContext.HostingEnvironment;
            
            bool reloadOnChange = hostingContext.Configuration.GetValue("hostBuilder:reloadConfigOnChange", defaultValue: true);

            config.AddTomlFile("appsettings.toml", optional: true, reloadOnChange: reloadOnChange)
                .AddTomlFile($"appsettings.{env.EnvironmentName}.toml", optional: true, reloadOnChange: reloadOnChange);
                
            if (env.IsDevelopment() && env.ApplicationName is { Length: > 0 })
            {
                var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
                if (appAssembly is not null)
                {
                    config.AddUserSecrets(appAssembly, optional: true, reloadOnChange: reloadOnChange);
                }
            }
            
            config.AddEnvironmentVariables();

            if (args is { Length: > 0 })
            {
                config.AddCommandLine(args);
            }
        })
        .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
        
// Console
var config = new ConfigurationBuilder()
    .AddTomlFile("appsettings.toml", optional: true, reloadOnChange: true)
    .Build();

appsettings.toml

AllowedHosts = "*"

[Logging]

    [Logging.LogLevel]
        Default = "Information"
        Microsoft = "Warning"
        "Microsoft.Hosting.Lifetime" = "Information"

or

AllowedHosts = "*"

[Logging.LogLevel]
Default = "Information"
Microsoft = "Warning"
"Microsoft.Hosting.Lifetime" = "Information"

Then you can access it like Configuration["Logging:LogLevel:Default"].

Binding with IOptions<TOptions> should work too.

Case sensitivity

TOML is case-sensitive but configuration keys aren't. So even though this is accepted when using Tomlyn alone, it's not accepted when using this library and exception will be thrown:

name = "Gina"
Name = "Gina"

Alternatives and benchmarks

Another possibly slightly faster alternative is Tommy.Extensions.Configuration.