ConfigSetting Layout Renderer in Various Environment
See original GitHub issueHi,
It looks like the NLog.Targets.ElasticSearch can only load config from appsetting.json as config setting layout renderer. I hope I am wrong, please correct me if it is possible to load other appsetting files with environment (ASPNETCORE_ENVIRONMENT). I have tried to put something like the following json in the appsetting.Development.json:
{
"ConnectionStrings": {
"ElasticUrl": "http://localhost:31114"
}
}
and running the project under the Development environment but unfortunately it is not respected, instead the value in appsetting.json is still used.
The project I am running is a .net core 2.2 web api and I am following the instruction here for the config setting layout renderer: https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer
Since it will automatically register hosting environment configuration with ConfigSettingLayoutRenderer, so I skip to do it manually. The .net core 2.2 framework handles the multiple environment appsetting files in behind, so I am not set it up explicitly.
Here is my code snippet to start the project (Program.cs):
public static void Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile("secret/appsettings.secret.json", optional: true, reloadOnChange: true)
.Build();
CreateWebHostBuilder(args, configuration).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args, IConfiguration config) =>
WebHost.CreateDefaultBuilder(args)
.UseConfiguration(config)
.ConfigureLogging((hostContext, configLogging) =>
{
configLogging.ClearProviders();
configLogging.SetMinimumLevel(LogLevel.Trace);
})
.UseNLog()
.UseStartup<Startup>();
I am not sure if I miss something to make this happen? Any help is appreciated.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8
NLog.Web.AspNetCore ver 4.9.3 has been released, and now you can do this:
var logger = NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();
That replaces the old style:
var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
Hi @snakefoot
Thanks for pointing this out.
The NLog.config did get loaded before initializing the ConfigSetting. My initialization logic is nearly intact compare with the new web api project expect I have replaced the log provider plus I have an additional log project with a legacy singleton logger class. The singleton class is obsoleted so that no one is using it any more. It’s been a bit wired to see this behavior.
I have read the both post you shared with me. Tried to load NLog config file as the way it did in #265, and that works for me. Actually I am happy with this solution because I can prepare different NLog config file for the according environment, and it is a way more clear than putting everything into the appsettings config files for me.
Now I am happy I can achieve that environment setting with this: