能否为NLog配置文件指定路径?

3
因为我使用Azure DevOps进行部署,所以我有3个环境(开发、预发布和生产)。每个环境都使用非常特定的NLog定义。
我想创建3个NLog文件Nlog.dev.config、NLog.staging.config和NLog.prod.config,并告诉NLog要使用哪一个。我也可以简单地将Nlog保存在不被部署工具覆盖的文件夹中。无论如何,我需要指定我的配置文件路径。

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


1
你可以明确地加载你想要的文件:https://github.com/NLog/NLog/wiki/Explicit-NLog-configuration-loading - Joe
2个回答

1

从文件中加载NLog配置 NLog将自动扫描配置文件,但有时平台需要显式配置加载,就像这样:

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

谢谢Joe。

感谢分享,您可以将其标记为答案。 - PatrickLu-MSFT

0

是的,根据情况有多种方法:

从代码中

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

或者在使用 Microsoft.Extensions.Logging 时从代码中调用(例如 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

或者从配置文件中创建一个 nlog.config 文件,并包含如下元素:

<?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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接