通用日志记录和特定日志记录(使用NLOG),重复日志记录

时间:2017-06-29 10:08:11

标签: c# nlog

我正在尝试创建2个记录器(使用NLog)

  1. 第一个记录器记录所有需要的项目以登录解决方案
  2. 另一个跟踪特定项目(我这样做是为了保持清洁和专注,并且只在这里运行trace
  3. 以下是配置

    <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <targets>
      <target name="logfile"
              xsi:type="File"
              layout="${longdate} ${level} ${threadid} ${callsite} ${message}"
              fileName="${basedir}\Logs\GatewayApplicationDebugAndErrorLog.txt"
              archiveNumbering="Rolling"
              maxArchiveFiles="10"
              archiveAboveSize="10000000"/>     
      <target name="J1939Trace"
              xsi:type="File"
              layout="${longdate} ${level} ${threadid} ${callsite} ${message}"
              fileName="${basedir}\Logs\J1939Trace.txt"
              archiveNumbering="Rolling"
              maxArchiveFiles="10"
              archiveAboveSize="10000000"/>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="logfile" />
      <logger name="J1939Trace" maxlevel="Trace" writeTo="J1939Trace" final="true" />
    </rules>
    

    用法如下所示

    private readonly Logger logger = LogManager.GetCurrentClassLogger(); // Generic Logger
    private readonly Logger j1939Logger = LogManager.GetLogger("J1939Trace"); // Specific Logger.
    

    我观察到的是特定记录器项也记录在通用日志项中,我不希望重复。有什么想法我做错了什么?

1 个答案:

答案 0 :(得分:1)

这会有用吗?

来自:NLog: How to exclude specific loggers from a specific rule?

添加final =&#34; true&#34;表示不会为&#34; SpammyLogger&#34;生成的事件执行更多规则,但它仅适用于指定的级别。(参见https://github.com/nlog/nlog/wiki/Configuration-file#rules

请务必仔细阅读答案中的所有评论。