-
Notifications
You must be signed in to change notification settings - Fork 0
When Layout Renderer
Julian Verdurmen edited this page Jun 28, 2016
·
10 revisions
Only outputs the inner layout when the specified condition has been met. See Conditions page.
Supported in .NET, Silverlight, Compact Framework and Mono.
##Configuration Syntax
${when:when=Condition:inner=Layout:else=Layout}
or by using ambient property to modify output of other layout renderer:
${other:when=Condition}
##Parameters ###Transformation Options
- when - Condition that must be met for the inner layout to be printed. Condition Required.
- inner - Wrapped layout. Layout
- else - Layout if the condition is not true (introduced in NLog 4.3.5)
##Examples
-
{message:when=logger=='logger'}
: print the message when the logger name is equal to "logger". -
${when:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}:inner=1:else:0}
convert a layout string result to a bit (1 or 0) that can be inserted into a SQL bit field.
##Escaping
When using :
and }
in a internal layout those characters need to be scape (there is no need to scape \
).
-
:
because it's a value separator. -
}
because it's the end of the layout
Working examples:
${when:when=1 == 1:Inner=Test\: Hello}
${when:when=1 == 1:Inner=Test\\Hello}
${when:when=1 == 1:Inner=Test\Hello}
${when:when=1 == 1:Inner=Test{Hello\}}
###Before 4.2
The colon (:) character should be wrapped within {literal:text=\:}
instead of placed directly within the inner layout.
Working Example
Configuration | layout="${when:when=1 == 1:inner=Test${literal:text=\:} Hello${literal:text=\:} World}" |
---|---|
Output | Test: Hello: World |
Non-working Example
Configuration | layout="${when:when=1 == 1:inner=Test: Hello: World}" |
---|---|
Output | World |
When the colon character is not wrapped only the last literal instance, in this case the word 'World', appears.
⭐ Workaround identified by: @reedyrm
- 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)