【问题标题】:Generic Logging and Specific Logging (using NLOG), Duplication of Logging通用日志记录和特定日志记录(使用 NLOG)、重复记录
【发布时间】:2023-03-11 11:36:08
【问题描述】:

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

  1. 第一个记录器记录所有所需的项目以登录解决方案
  2. 另一个跟踪特定项目(我这样做是为了让事情保持干净和专注,只在此处运行trace

下面是配置

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

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

【问题讨论】:

  • 意图是一个日志是trace,另一个日志在trace吗?您可以将通用记录器更改为调试吗?
  • @derek 这不是本意。记录除特定记录器中记录的内容之外的所有内容的意图 ID。

标签: c# nlog


【解决方案1】:

这行得通吗?

发件人:NLog: How to exclude specific loggers from a specific rule?

添加 final="true" 意味着不再对“SpammyLogger”产生的事件执行规则,但它只适用于指定的级别。(参见https://github.com/nlog/nlog/wiki/Configuration-file#rules

确保通读答案中的所有 cmets。

【讨论】:

  • 这个有效,顺序很重要!我没看到。
猜你喜欢
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2011-06-17
  • 2013-10-22
  • 1970-01-01
相关资源
最近更新 更多