Config examples with filters

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Config examples with filters

TheMaverick
Hello!

Could someone provide me an config example with some filter rules (whenContains would be nice :) ) ?
I have tried it, but all I produce are runtime exceptions ... :(

Thanks for readin' !
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Config examples with filters

Jarek Kowalski
Administrator
Try this config file:

<?xml version="1.0" ?>
<nlog>
    <targets>
        <target name="console" type="Console" layout="${logger} ${message}" />
    </targets>
    <rules>
        <logger name="*" minlevel="Debug" writeTo="console">
            <filters>
                <whenContains layout="${message}" substring="zzz" action="Ignore" />
            </filters>
        </logger>
    </rules>
</nlog>

This sample code:

using NLog;
using NLog.Targets;
using NLog.Targets.Wrappers;

class Example
{
    static void Main(string[] args)
    {
        Logger logger = LogManager.GetLogger("Example");
        logger.Debug("log message");
        logger.Debug("log zzz message");
    }
}

produces only "Example log message" (the second log message is filtered out by the <whenContains />

Alternatively you could use the new feature called Conditions (available in the latest snapshots) which let you write filters using a simple notation:

<filters>
    <when condition="contains(message,'zzz')
              or level >= LogLevel.Warn
              or level == LogLevel.Trace" action="Ingore" />
</filters>
NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Config examples with filters

TheMaverick
Thanks!

Now I'm aware why the layout attribute is there :)
Loading...