-
Notifications
You must be signed in to change notification settings - Fork 0
Conditions
Jeremy Cook edited this page Aug 7, 2015
·
16 revisions
Conditions are filter expressions used with the when filter. They consist of one or more tests. They are used in the when filter to determine if an action will be taken.
##Language The filter expressions are written in a special mini-language. The language consists of:
- relational operators:
==
,!=
,<
,<=
,>=
and>
Note: Some predefined XML characters may need to be escaped. For example, if you try to use the '<' character, the XML parser will interpret it as an opening tag which results in an error in the configuration file. Instead, use the escaped version of '<' (<
) in this context. - boolean operators:
and
,or
,not
- string literals which are always evaluated as layouts -
${somerenderer}
- boolean literals -
true
andfalse
- numeric literals - e.g.
12345
(integer literal) and12345.678
(floating point literal) - log level literals -
LogLevel.Trace
,LogLevel.Debug
, ...LogLevel.Fatal
- predefined keywords to access the most common log event properties -
level
,message
andlogger
- braces - to override default priorities and group expressions together
- condition functions - to perform
string
andobject
tests
##Functions The following condition functions are available:
-
contains(s1,s2)
Determines whether the second string is a substring of the first one. Returns:true
when the second string is a substring of the first string,false
otherwise. -
ends-with(s1,s2)
Determines whether the second string is a suffix of the first one. Returns:true
when the second string is a prefix of the first string,false
otherwise. -
equals(o1,o2)
Compares two objects for equality. Returns:true
when two objects are equal,false
otherwise. -
length(s)
Returns the length of a string. -
starts-with(s1,s2)
Determines whether the second string is a prefix of the first one. Returns:true
when the second string is a prefix of the first string,false
otherwise.
##Examples Here are several when filter examples with conditions:
<rules>
<logger name="*" writeTo="file">
<filters>
<when condition="length('${message}') > 100" action="Ignore" />
<when condition="equals('${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" />
</filters>
</logger>
</rules>
##Extensibility
New condition functions are easy to add; just create 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
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.netfx35.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<extensions>
<add assembly="NLog.Extensions" />
</extensions>
...
<nlog>
- Search in documentation
- Getting started
- Examples
- External articles and tutorials
- FAQ
- Platform support
- Advanced Configuration file options
- Filtering log messages
- [Using Time Sources](Time Source)
- Visual Studio support
- Who is using NLog
- [Write custom extensions](Extending NLog)