【问题标题】:LogManager.Configuration.FindRuleByName return null. NLogLogManager.Configuration.FindRuleByName 返回 null。日志
【发布时间】:2020-11-16 12:14:41
【问题描述】:

我有规矩

<targets >
    <target name="Global" xsi:type="File" fileName="Logs/GlobalLog.txt"  archiveNumbering="Date" enableArchiveFileCompression="true" archiveFileName="Archive/IPSlog.{#}.zip" archiveDateFormat="yyyy-MM-dd" 
     maxArchiveDays="${logLifetime}"
    archiveEvery="Day" />
  </targets>
 <rules>
    <logger name="Global" minlevel="Trace"  writeTo="Global,Color" />
  </rules>

我尝试从代码更改规则

 LogManager.Configuration.FindRuleByName("Global").EnableLoggingForLevels(loglevel, LogLevel.Fatal);

但 FindRuleByName 返回 null。我的错误在哪里? 目标查找器工作正常

    var target = (FileTarget)LogManager.Configuration.FindTargetByName("Global");

【问题讨论】:

    标签: c# .net xml nlog


    【解决方案1】:

    这条规则:

    <logger name="Global" minlevel="Trace"  writeTo="Global,Color" />
    

    指定以下条件:

    • 记录器必须命名为“全局”。前任。 NLog.LogManager.GetLogger("Global")
    • Lo​​gLevel 必须是 Trace(或更多服务器)
    • 写入名为"Global""Color" 的目标。

    我猜你想要的是这个:

    <logger name="*" minlevel="Trace"  writeTo="Global,Color" ruleName="Global" />
    
    • 由于*-wildcard,Logger 可以是任何名称。前任。 NLog.LogManager.GetCurrentClassLogger()
    • Lo​​gLevel 必须是 Trace(或更多服务器)
    • 写入名为"Global""Color" 的目标。
    • 分配 Logging-Rule-Name,因此可以使用 FindRuleByName 进行查找。

    请注意,您还可以使用 NLog Config-variables 或 NLog Global-Diagnostic-Context 动态更新级别过滤器。也可以看看: https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 2014-11-30
      • 2020-10-04
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多