You can provide configuration from different places in .NET Core: JSON file, INI file, environment variables, ...
But how to debug what configuration is coming from where? GetDebugView() to the rescue!
appsettings.json:
{
"myconfiguration": "foo"
}
appsettings.ini:
myconfiguration=bar
Environment variable:
$ export myconfiguration=qux
Program.cs
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
var configuration = new ConfigurationBuilder() // Microsoft.Extensions.Configuration package
.SetBasePath(Directory.GetCurrentDirectory()) // Microsoft.Extensions.Configuration.FileExtensions package
.AddJsonFile("appsettings.json") // Microsoft.Extensions.Configuration.Json package
.AddIniFile("appsettings.ini") // Microsoft.Extensions.Configuration.Ini package
.AddEnvironmentVariables() // Microsoft.Extensions.Configuration.EnvironmentVariables package
.Build();
// Displays the value from the environment values, or from the INI file, or from the JSON file.
// Follows the precedence of the configuration above.
Console.WriteLine(configuration["myconfiguration"]);
// Display all configurations key and values and the provider of such configuration.
Console.WriteLine(configuration.GetDebugView());
Top comments (0)