Xml配置中的NLog default-target-parameters不适用于以编程方式添加的文件目标

时间:2018-07-25 09:08:23

标签: nlog

我正在研究.Net Core 2.0控制台应用程序。 NLog版本4.5.7。 这是我的配置文件:

<?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"
    throwExceptions="false"
    internalLogFile="Logs/Internal/log.txt"
    internalLogLevel="Warn"
    internalLogToConsole="false"
    internalLogToConsoleError="false"
    internalLogToTrace="false"
    internalLogIncludeTimestamp="true">

    <targets>
      <default-wrapper
        xsi:type="AsyncWrapper"
        timeToSleepBetweenBatches="0"
        overflowAction="Block"/>

      <default-target-parameters
        xsi:type="File" 
        layout="${longdate}|${level:uppercase=true}|${message}
          ${onexception:${newline}}${exception:format=tostring}"
        archiveAboveSize="10485760"
        maxArchiveFiles="10"
        archiveNumbering="DateAndSequence"
        archiveOldFileOnStartup="true"
        keepFileOpen="true"
        cleanupFileName="false"/>

      <target
        name="driver"
        xsi:type="File"
        archiveFileName="Logs/Driver/Archives/log.{#####}.txt"
        fileName="Logs/Driver/log.txt"/>
    </targets>

    <rules>
      <logger
        name="driver"
        minlevel="Trace"
        writeTo="driver"/>
    </rules>
  </nlog>

我正在启动应用程序时设置此配置:

LogManager.Configuration = new XmlLoggingConfiguration(<path>, false);

然后我以编程方式将文件目标添加到此配置中:

string
  deviceTag = string.Concat("Device_", _id.ToString()),
  loggerName = string.Concat("logger_", deviceTag);

LogManager.Configuration.AddRuleForAllLevels(
  new FileTarget(string.Concat("target_", deviceTag))
  {
    FileName = string.Concat("Logs/", deviceTag, "/log.txt"),
    ArchiveFileName = string.Concat("Logs/", deviceTag,
      "/Archives/log.{#####}.txt")
  }, loggerName);

_logger = LogManager.GetLogger(loggerName);
LogManager.ReconfigExistingLoggers();

目标正在运行,日志已写入。但是会忽略xml config中的default-target-parameters。 这是错误还是功能? 还是我做错了什么?

0 个答案:

没有答案