Conditions

Conditions are used in various places to filter log events based on their content.

Condition language

NLog supports a simple language to express conditions. The language consists of:

  • relational operators (==, !=, <=, <=, >= and >)
  • and, or, not boolean operators
  • string literals which are always evaluated as layouts - '${somerenderer}'
  • boolean literals - true and false
  • log level literals - LogLevel.Trace, LogLevel.Debug, ... LogLevel.Fatal
  • numeric literals - 12345 (integer literal) and 12345.678 (floating point literal)
  • predefined keywords to access the most common log event properties - level, message and logger
  • braces - to override default priorities and group expressions together

Examples

Here are some examples of conditions:

  • level > LogLevel.Debug - matches the messages whose level is greater than Debug
  • (level > LogLevel.Debug) or contains(message,'xxx') - matches the messages whose level is greater than Debug or which include the xxx substring in the log message
  • starts-with(logger,'Kopytko.') - matches the loggers whose names start with Kopytko.
  • ends-with(logger,'.SQL') or ends-with(logger,'.XML') - matches the loggers whose names end with either .SQL or .XML
  • true - matches everything
  • false - matches nothing
  • length(message) > 100 - matches the log events where the length of the log message is greater than 100
  • '${shortdate}' == '2005-11-10' - matches on the specified date

Available functions

The following functions are available:

NameDescriptionDefined in
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.NLog.dll
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.NLog.dll
equals(o1,o2) Compares two objects for equality. Returns: true when two objects are equal, false otherwise.NLog.dll
length(s) Returns the length of a string. Returns: The length of a string.NLog.dll
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.NLog.dll