简体   繁体   English

NLog 不包含过滤器似乎不起作用

[英]NLog not contains filter doesn't seem to work

I am using NLog 4.6.8 and have the following simple NLog.config and Program.cs我正在使用 NLog 4.6.8 并有以下简单的 NLog.config 和 Program.cs

NLog.config配置文件

<?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">

  <variable name="consoleLayout" value="${longdate} [${threadid}] ${logger} ${uppercase:${level}} ${message} ${exception:format=tostring}"/>

  <targets>
    <target type="Console" name="c" layout="${consoleLayout}" />
  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="c">
      <when condition="not contains('${message}','XXX')" action="Ignore"/>
     </logger>
  </rules>
</nlog>

Program.cs程序.cs

using System;

namespace ConsoleApp1
{
    public class Program
    {
        private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public static void Main(string[] args)
        {
            logger.Debug("Some dd message");
            logger.Info("Some ii message");
            logger.Debug("XXX Some dd message");
            logger.Info("XXX Some ii message");

            Console.ReadLine();
        }
    }
}

I am expecting the bottom two lines to show up in my console log only and not the first two, but this is what I get:我希望底部的两行只显示在我的控制台日志中,而不是前两行,但这就是我得到的:

2020-03-17 12:56:31.3767 [1] ConsoleApp1.Program DEBUG Some dd message 
2020-03-17 12:56:31.4046 [1] ConsoleApp1.Program INFO Some ii message
2020-03-17 12:56:31.4076 [1] ConsoleApp1.Program DEBUG XXX Some dd message
2020-03-17 12:56:31.4076 [1] ConsoleApp1.Program INFO XXX Some ii message

This seems so simple, but yet it doesn't work, I'm probably missing something stupid.这看起来很简单,但它不起作用,我可能错过了一些愚蠢的东西。 Please help.请帮忙。

Think you need to write it like this (Include filters)认为你需要这样写(包括过滤器)

  <rules>
    <logger name="*" minlevel="Debug" writeTo="c">
       <filters>
         <when condition="not contains('${message}','XXX')" action="Ignore"/>
       </filters>
     </logger>
  </rules>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM