NLog 配置未按预期工作

NLog configuration not working as expected

我有如下的NLog配置文件

<?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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">

  <!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
  <variable name="myvar" value="myvalue"/>

  <!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
    -->
  <targets>
    <target
      name="logfile"
      xsi:type="File"
      fileName="log.txt"
      archiveFileName="log.{#}.txt"
      archiveNumbering="DateAndSequence"
      archiveAboveSize="1000000"
      archiveDateFormat="yyyyMMdd"
    />
    <target
      name="byteArrayLogfile"
      xsi:type="File"
      fileName="log.ba.txt"
      archiveFileName="log.ba.{#}.txt"
      archiveNumbering="DateAndSequence"
      archiveAboveSize="1000000"
      archiveDateFormat="yyyyMMdd"
      />
    <target 
      name="console" 
      xsi:type="Console" />
  </targets>


  <rules>
    <logger name="StackTraceLogger" minlevel="Warn" maxlevel="Error" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${message}${newline}${exception:format=tostring}" final="true"/>
    <logger name="ByteArrayLogger" minlevel="Debug" writeTo="byteArrayLogfile" layout="${longdate}|${threadid}|${level:uppercase=true}${newline}${message}" final="true"/>
    <logger name="Ctrack.DMT.*" minlevel="Debug" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
    <logger name="Ctrack.DMT.*" minlevel="Trace" writeTo="console" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
    <logger name="ServerForm.*" minlevel="Debug" writeTo="logfile" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
    <logger name="ServerForm.*" minlevel="Trace" writeTo="console" layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${logger}|${message}" />
  </rules>
</nlog>

目的是:

  1. 从 class 库或跟踪级别的 winform 发送任何内容 + 到控制台
  2. 将 class 库或调试级别的 winform 中的所有内容发送到日志文件
  3. 将任何内容从 ByteArrayLogger 发送到 byteArrayLogfile
  4. 使用略有不同的布局将 ExceptionLogger 中的所有内容发送到日志文件

每个 class 都有以下的顶部:

private static Logger logger = LogManager.GetCurrentClassLogger();
private static Logger loggerEL = LogManager.GetLogger("ExceptionLogger");
private static Logger loggerBA = LogManager.GetLogger("ByteArrayLogger");

记录异常的步骤

string message = "Exception while trying to save " + Name + " to the database.";
logger.Error("{0}|{1}", MethodName, message);
loggerEL.Error(ex, "{0}|{1}", MethodName, message);

Stacktraces 可能会在不久的将来移出到一个单独的文件中

无效的东西

此外,Intellisense 正在突出显示布局并说 The 'layout' attribute is not declared 作为警告。

In addition, Intellisense is hilighting layout and saying The 'layout' attribute is not declared as a warning.

在这种情况下,这也是错误。 layout 应该在 <target> 而不是 <logger>

所以

 <targets>
    <target
      name="logfile"
      xsi:type="File"
      fileName="log.txt"
      archiveFileName="log.{#}.txt"
      archiveNumbering="DateAndSequence"
      archiveAboveSize="1000000"
      archiveDateFormat="yyyyMMdd"
 layout="${longdate}|${threadid}|${callsite-linenumber:skipFrames=Integer}|${level:uppercase=true}|${message}${newline}${exception:format=tostring}"
    />
   ...

  <rules>
    <logger name="StackTraceLogger" minlevel="Warn" 
            maxlevel="Error" writeTo="logfile" final="true" />
     ...
 </rules>