❔ App.config: "special one" for testing/debugging

Hello, I'm making a small console application that needs some configuration (an API key, and a list of "filters"). I thought of saving that kind of configuration in an App.config file. Question is: how can I just have a "template" file for VCS storage, and have a "local" one filled with my secrets for testing/debugging?
10 Replies
Kosta
Kosta2y ago
Use multiple environments in ASP.NET Core
Learn how to control app behavior across multiple environments in ASP.NET Core apps.
Relevant
Relevant2y ago
App.Debug.config
LordKalma (CT7ALW)
I'm not using ASP.NET, but thanks. Care to develop a bit?
Relevant
Relevant2y ago
Oh yeah, sorry. What do you have so far? Have you implemented any Configuration files yet?
Relevant
Relevant2y ago
Ah, I see. This is sort of the .net framework way of doing thing, using ConfigurationManager. This is no longer the standard way of doing things. Modern way of doing it would be to use appsettings.json. This could be implemented in a console app like:
var configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json");

var config = configuration.Build();
var configuration = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json");

var config = configuration.Build();
and to do something that was environment specific,
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");


var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
.AddEnvironmentVariables();
var configuration = builder.Build();
var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");


var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environmentName}.json", true, true)
.AddEnvironmentVariables();
var configuration = builder.Build();
LordKalma (CT7ALW)
Interesting, thanks App.config was the first thing I found in the docs, so I went for that I need to improve that anyway
LordKalma (CT7ALW)
I started using C# yesterday (and I already have this working, this language is so productive!) Thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server