A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/nlog/nlog/wiki/ConfigSetting-Layout-Renderer below:

ConfigSetting Layout Renderer · NLog/NLog Wiki · GitHub

Value from the appsettings.json or other configuration in .NET Core

Introduced with NLog.Extensions.Logging 1.4.0 and NLog.Web.AspNetCore 4.8.0

When calling UseNLog() from NLog.Web.AspNetCore or NLog.Extensions.Hosting then it will automatically register hosting environment configuration with ConfigSettingLayoutRenderer.

But to make ${configsetting} work during startup, before Host has been built, then NLog.Web.AspNetCore makes it easier to load appsettings.json (Since ver. 4.9.3):

// Loads appsetting.json and enables ${configsetting}
var logger = LogManager.Setup()
                       .LoadConfigurationFromAppSettings()
                       .GetCurrentClassLogger();

Manual loading of appsettings.json with fluent API (Available with NLog v5):

IConfigurationRoot config = new ConfigurationBuilder()
    .AddJsonFile(path: "AppSettings.json").Build();
var logger = LogManager.Setup()
                       .LoadConfigurationFromSection(config)
                       .GetCurrentClassLogger();

To manual register the Microsoft Extension IConfiguration with ${configsetting} for NLog v4:

IConfigurationRoot config = new ConfigurationBuilder()
    .AddJsonFile(path: "AppSettings.json").Build();
NLog.Extensions.Logging.ConfigSettingLayoutRenderer.DefaultConfiguration = config;
${configsetting:item=String:default=String}

Example: appsettings.json:

{
    "Mode":"Prod",
    "Options":{
        "StorageConnectionString":"UseDevelopmentStorage=true",
    }
}

Config Setting Lookup:

${configsetting:item=Mode} // renders "Prod"
${configsetting:item=Options.StorageConnectionString} // renders "UseDevelopmentStorage=true"
${configsetting:item=Options.TableName:default=MyTable} // renders "MyTable"

Config Setting Lookup Cached:

${configsetting:cached=True:item=Mode}

Notice appsettings.json gives the ability to make environment-specific overrides (Ex. appsettings.Production.json). See also Environment-specific-NLog-Logging-Configuration


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4