A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/nlog/nlog/wiki/When-filter below:

When Filter · NLog/NLog Wiki · GitHub

Filter events in the config.

Platforms Supported: All

<rules>
  <logger ... >
    <filters defaultAction="Neutral | Ignore | Log | LogFinal | IgnoreFinal">
      <when condition="Condition" action="Enum"/>
    </filters>
  </logger>
</rules>

Conditions are expressions used with the <when> filter. They consist of one or more tests. They are used in the filter to determine if an action will be taken.

The filter expressions are written in a special mini-language. The language consists of:

The following condition functions are available:

Single quotes should be escaped with another single quote. Example:

contains('${message}', 'Cannot insert the value NULL into column ''Col1')

New custom condition functions methods can also be added.

NLog 4.7 allows you to register your own condition methods using LogFactory.Setup(). Where a lambda can be registered like this:

LogManager.Setup().SetupExtensions(s => 
   s.RegisterConditionMethod("hasParameters", evt => evt.Parameters?.Length > 0)
);

And a static method like this:

LogManager.Setup().SetupExtensions(s =>
   s.RegisterConditionMethod("hasPriority", typeof(NLogConditionMethods).GetMethod("HasPriority", BindingFlags.Static))
);

Alternative way of adding custom condition methods is creating a public static class with a static function and mark the class and method with the attributes [ConditionMethods] and [ConditionMethod] respectively.

You can find a sample implementation of a custom filter here

Then you have to tell NLog where to find your assembly (Ex. NLog.ConditionMethodsAssembly.dll)

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.netfx35.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
    <extensions>
		<add assembly="NLog.ConditionMethodsAssembly" />
    </extensions>
   ...
<nlog>

Here are several examples with conditions:

<rules>
    <logger name="*" minLevel="Debug" writeTo="file">
        <filters defaultAction="Log">
            <when condition="exception != null" action="Log" />
            <when condition="length(message) > 100" action="Ignore" />
            <when condition="'${OnHasProperties:1}' != ''" action="Ignore" />
            <when condition="logger == 'MyApps.SomeClass'" action="Ignore" />
            <when condition="(level >= LogLevel.Debug and contains(message, 'PleaseDontLogThis')) or level==LogLevel.Warn" action="Ignore" />
            <when condition="not starts-with(message, 'PleaseLogThis')" action="Ignore" />
            <when condition="contains(message, '&quot;Bob&quot;')" action="Ignore" />  <!-- "Bob" -->
        </filters>
    </logger>
</rules>

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4