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.

ConfigSetting Layout Renderer in Various Environment

See original GitHub issue

Hi,

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:closed
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
snakefootcommented, Aug 8, 2020

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();

1reaction
maximdxcommented, Jul 10, 2019

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:

            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
                .AddJsonFile("secret/appsettings.secret.json", optional: true, reloadOnChange: true)
                .Build();
            ConfigSettingLayoutRenderer.DefaultConfiguration = configuration;
            NLogBuilder.ConfigureNLog($"NLog.{environment}.config").GetCurrentClassLogger();
            CreateWebHostBuilder(args, configuration).Build().Run();
Read more comments on GitHub >

github_iconTop Results From Across the Web

NLog config settings renderer not working as expected
I have a configuration section in my appsettings to provide environment specific parameters. "LoggingContext": { .. "ApplicationName": "cool-app ...
Read more >
Config options for NLog's configuration
The following types can be configured: Targets - the destinations of a logevent, e.g. file, database, console; Layout - the layout e.g. json,...
Read more >
ConfigSetting Layout Renderer - NLog/NLog GitHub Wiki
When calling UseNLog() from NLog.Web.AspNetCore or NLog.Extensions.Hosting then it will automatically register hosting environment configuration ...
Read more >
Logging to elmah.io from NLog
Log messages to elmah.io, just as with every other target and NLog: ... Splitting up configuration like that is supported through NLog layout...
Read more >
Setting the NLog database connection string in the ASP.NET ...
This article shows how the NLog connection string for the DatabaseTarget can be configured in the appsettings.json in an ASP.
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