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

Environment specific NLog Logging Configuration

Rolf Kristensen edited this pageAug 9, 2025 ·36 revisions

Deploy NLog configuration file

NLog scans for its configuration file atmultiple file locations. By making sure the installer-process deploys the environment specific NLog configuration-file to the install-folder, then NLog will load it automatically.

There also exists XML transformation tools, that can take a single XML-file and transform it to an environment-specific result-file (As part of application installation).

Alternative approach is having a basic NLog.config file, and use theinclude-feature to apply environment-specific-overrides like this:

<nlog>    <variablename="flushTimeout"value="60000" />    <includefile="nlog.local.config"ignoreErrors="true" />    <targets>       <targettimeout="${flushTimeout}" />    </targets></nlog>

And then deploynlog.local.config that only contains the environment-specific overrides:

<nlog>    <variablename="flushTimeout"value="30000" /></nlog>

Include NLog configuration file

If the installer-process cannot decide what NLog configuration-file to deploy, then one can consider using theinclude-feature of the NLog configuration-file. Then one can have a basic NLog.config with the following contents:

<nlog>     <includefile="nlog_${environment:DOTNET_ENVIRONMENT}.config"ignoreErrors="true" /></nlog>

It is also possible to setup NLogGlobal Diagnostic Context (GDC) and use${gdc:Environment} inside theinclude="...". Make sure to make the setup before creating the first NLog Logger (as it will automatically load the NLog-configuration).

Explict load NLog configuration

It is possible to skip the automatic loading of NLog configuration by just explicit assigning the NLog configuration. Then one can load the configuration from any file-path:

NLog.LogManager.Setup().LoadConfigurationFromFile("D:/Config/NLog.LIVE.config");

Or load NLog configuration as XML-string, that have been retrieved from remote location/database:

NLog.LogManager.Setup().LoadConfigurationFromXml(xmlString);

See also:Explicit-NLog-configuration-loading

Setup with LoadConfigurationFromAppSettings

When using NLog.Web.AspNetCore then it is recommended to setup NLog Logging configuration early like this:

varlogger=NLog.LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();logger.Debug("init main");

This will automatically make NLog check these additional file-locations:

  • Looks for "NLog"-section in theappsettings.json file (with override fromappsettings.Production.json or environment variables)
  • Looks fornlog.{environment}.config file, where{environment} can be specified as input-parameter toLoadConfigurationFromAppSettings() or automatically extracted from the machine environment-variables (Ex.DOTNET_ENVIRONMENT)

Resolve configuration from environment

TheNLog Layouts makes it possible to resolve option-values from external sources. Instead of having environment-specific details as part of the NLog-configuration, then one resolve the value from other source. Ex:

  • ${environment} - From machine environment variables.
  • ${configsetting} - From appsettings.json (with override fromappsettings.Production.json or environment variables)
  • ${appsetting} - From app.config with .NET Framework.
  • ${gdc} - From NLog Global Diagnostic Context (GDC), that can be assigned at early stage in the application.
  • OtherLayout Renderers

This allows you to place environment specific Urls / API-keys / etc. in environment-specific configuration-files (or other places).

It can be useful to use:whenEmpty=DefaultValue to all specify a fallback value, if no value is available. Ex.${environment:COMPANY_NAME:whenEmpty=Contoso}.

NLog configuration can also be loaded fromappsettings.json, where Microsoft Extension Configuration System supports overrides of the defaultappsettings.json (Ex.appsettings.Production.json or environment variables)

Conditional NLog Target filters

Instead of maintaining multiple NLog configuration files, then one can consider just have a single NLog.config with conditional target output.

<nlog>   <variablename="myLevel"value="Warn" />    <rules>      <loggerminLevel="${var:myLevel}"writeTo="live_target" />    </rules></nlog>

Then depending on the application environment then relevant targets output will be enabled. Ex:

#ifDEBUGLogManager.Configuration.Variables["myLevel"]="Off";LogManager.ReconfigExistingLoggers();// Explicit refresh of Layouts and updates active Logger-objects#endif

See also:Semi Dynamic Routing Rules

-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