question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: appsettings.json and appsettings.environment.json for ASP.NET Core

See original GitHub issue

ASP.NET Core appsettings.json files can be used additively for different environments, e.g.:

.AddJsonFile("appsettings.json", optional:false)
.AddJsonFile("appsettings.Development.json", optional:true)

The latter adds to the former.

Let’s say I always want file sink, but console sink only in development. So I add file sink to appsettings.json and console sink to appsettings.Development.json.

But in development, the result is that file sink is ignored. If I want it then I must add it to both files, which is a bad idea as copy-paste mistakes will happen one day.

Am I doing this correctly?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
skomis-mmcommented, Jul 28, 2018

@lonix1 json array syntax is a syntatic sugar here.

"WriteTo": [
  { "Name": "File", "Args": { "path": "log.txt" } }
]

is equal to:

`"WriteTo:0":  {
   "Name": "File", "Args": { "path": "log.txt" } 
}

where 0 is element index in the array. Microsoft.Extension.Configuration is build around key-value dictionary so each configuration key must have unique path. So in your case you overwrite the first element in the appsettings.Development.json. Workaround will be any other name (not 0):

"WriteTo:1": {
   "Name": "Console" 
}
"WriteTo:MyConsole": {
   "Name": "Console" 
}
"WriteTo:Whatever": {
   "Name": "Console" 
}
0reactions
lonix1commented, Aug 27, 2018

Problem is it now gives System.InvalidOperationException: The configuration value in Serilog:WriteTo:0:Name has no 'Name' element.

However when I remove the array then it works. Since I’m only using one sink in that configuration, it’s good enough for me. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Automatically set appsettings.json for dev and release ...
First, add a key to your launch.json file. · Then, in your project, create a new appsettings.{environment}. · And finally, configure it to...
Read more >
How to create environments with appSettings?
How to create environments with appSettings? · Add a appsettings.Home.json file to the project. · Add the setting to the file, for example:...
Read more >
Configuration in ASP.NET Core
Environment variable names reflect the structure of an appsettings.json file. Each element in the hierarchy is separated by a double underscore ...
Read more >
ASP.NET Core AppSettings.json file
The appsettings.json file is the application configuration file that is used to store configuration settings such as database connection strings, any ...
Read more >
Multiple appsettings.json in .net core without using an ...
This idea is great if you have one server dedicated to one . net core application, which is usually not the case.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found