Is it possible to specify a path for NLog config file?

Viewed 648

Because I use Azure DevOps my deployment and I have 3 environments (dev, staging, prod). Each of them use really specific NLog definition.

I would like to create 3 NLog file Nlog.dev.config, NLog.staging.config and NLog.prod.config and tell NLog which one to use. I can also simply save Nlog in a folder that is not overridden by the deployment tool. Whatever, I need to specify the path of my config file.

https://github.com/nlog/NLog/wiki/Configuration-file

2 Answers

Loading NLog configuration from file NLog will automatically scan for configuration files, but sometimes the platform requires explicit configuration loading like this:

NLog.LogManager.Configuration = new NLog.Config.XmlLoggingConfiguration("nlog.config");

Thank you Joe

Yes, there are multiple ways dependent of the situation:

From code

LogManager.Configuration = new XmlLoggingConfiguration(@"c:\path\to\NLog.config")

Or from code when using Microsoft.Extensions.Logging (e.g. asp.net core)

public static void Main(string[] args)
{
    var logger = NLog.Web.NLogBuilder.ConfigureNLog(@"c:\path\to\NLog.config").GetCurrentClassLogger();
    ... // Etc, see https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3

Or from config you could create a nlog.config file with an include element:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      internalLogLevel="Info"
      internalLogFile="c:\temp\internal-nlog.txt">

    <include file="nlog-common.config" />
</nlog>
Related