How can I read the URL value from the below shown appsettings.json file, without creating a class to load values through DI?
{
"Serilog": {
"WriteTo": [
{
"Name": "Seq",
"Args": {
"serverUrl": "http://localhost:1234"
}
}
]
}
}
I tried the below snippet but it gives null.
var configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false)
.AddJsonFile($"appsettings.{environmentName}.json", optional: true)
.AddEnvironmentVariables();
IConfiguration _configuration = configurationBuilder.Build();
// 1st attempt
var url = _configuration.GetSection("Serilog").GetChildren().First()["Args::serverUrl"];
// 2nd attempt
var url = _configuration.GetValue<string>("Serilog:WriteTo:Args::serverUrl");
// 3rd attempt
var url = _configuration.GetSection("Serilog").GetChildren().First().GetSection("Args").GetValue("serverUrl");
None of those attempts worked.
There are some existing similar questions but none of them deal with getting a single value from collection.
I'm sorry im not really familiar with "those words". English isn't my first language.
This here is your corrected 2nd try, without any dependencys/classes:
You just had a little typo thus not accessing the key right.