DEV Community

loading...
Cover image for From where that configuration is coming from?

From where that configuration is coming from?

tallesl profile image Talles L ・1 min read

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"
}
Enter fullscreen mode Exit fullscreen mode

appsettings.ini:

myconfiguration=bar
Enter fullscreen mode Exit fullscreen mode

Environment variable:

$ export myconfiguration=qux
Enter fullscreen mode Exit fullscreen mode

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());
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide