Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/NLogPublic

ConfigSetting Layout Renderer

Rolf Kristensen edited this pageOct 26, 2025 ·20 revisions

Lookup value from the appsettings.json or appsettings.Production.json and other configuration sources/overrides.

Introduced withNLog.Extensions.Logging 1.4.0 andNLog.Web.AspNetCore 4.8.0

Configuration Syntax

${configsetting:item=String:default=String}

Parameters

Rendering Options

  • item - Key in the config. Required. Use. for nested objects.
  • Default - Default value if not present. Optional.

Setup ConfigSetting

${configsetting} depends on Microsoft.Extensions.Configuration being loaded and available, so if usingUseNLog() orAddNLog() on HostBuilder then everything will work out-of-the-box.

But if needing to load NLog configuration early before HostBuilder is ready, then${configsetting} will not resolve the expected values, which might cause NLog Targets not using the expected configuration values. It is possible to setup${configsetting} manually for these situations.

NLog Fluent Setup usingLoadConfigurationFromAppSettings() fromNLog.Web.AspNetCore nuget-package:

varlogger=LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();

NLog Fluent Setup usingLoadConfigurationFromSection(() fromNLog.Extensions.Logging nuget-package:

IConfigurationRootconfig=newConfigurationBuilder().AddJsonFile(path:"AppSettings.json").Build();varlogger=LogManager.Setup().LoadConfigurationFromSection(config).GetCurrentClassLogger();

NLog v4 can perform manual registration like this withNLog.Extensions.Logging nuget-package:

IConfigurationRootconfig=newConfigurationBuilder().AddJsonFile(path:"AppSettings.json").Build();NLog.Extensions.Logging.ConfigSettingLayoutRenderer.DefaultConfiguration=config;

Examples

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}

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

-Troubleshooting Guide - See available NLog Targets and Layouts:https://nlog-project.org/config

Configuration

Programmatic Configuration

Advanced

Extending NLog

Clone this wiki locally


[8]ページ先頭

©2009-2025 Movatter.jp