【问题标题】:How to add filter condition for NLog Target如何为 NLog Target 添加过滤条件
【发布时间】:2021-04-28 11:50:53
【问题描述】:

我有一个问题,我写了一个 Nlog.target 来登录数据库,但我希望 nlog 应该根据条件记录。如果 dbvalue 为真,我想设置一个条件,然后在 db 中创建日志条目。 我写了下面的代码,它不起作用,dbvalue 是 false,但日志仍然在 db 中生成。

我在我的 startup.cs 文件中添加了这一行 GlobalDiagnosticsContext.Set("dbvalue", "false");

这是我的 nlog.config

<rules>
   <logger name="Microsoft.*" maxlevel="Info" final="true" writeTo="" />
 
   <logger name="*" minlevel="Info" writeTo="database" >
      <filters>
         <when condition="equals('${gdc:dbvalue}', 'true')" action="Log" />
      </filters>
   </logger>
</rules>

有人可以帮忙吗?

【问题讨论】:

标签: c# asp.net-core nlog


【解决方案1】:

我可能会这样做:

<rules>
   <logger name="Microsoft.*" maxlevel="Info" final="true" writeTo="" />
 
   <logger name="*" minlevel="${gdc:dbLogMinLevel:whenEmpty=Off}" writeTo="database" />
</rules>

然后像这样启用数据库记录器:

NLog.GlobalDiagnosticsContext.Set("dbLogMinLevel","Info");
NLog.LogManager.ReconfigExistingLoggers(); // Explicit refresh active Logger-objects

另请参阅:https://github.com/NLog/NLog/wiki/Filtering-log-messages#semi-dynamic-routing-rules

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    相关资源
    最近更新 更多