DEV Community

Cover image for From where that configuration is coming from?
Talles L
Talles L

Posted on

From where that configuration is coming from?

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

Top comments (0)