I am using NLog 4.6.8 and have the following simple NLog.config and 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
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 defaultAction="Ignore">
<when condition="contains('${message}','XXX')" action="Log"/>
</filters>
</logger>
</rules>