Tony
Tony

Reputation: 2066

NLog: How to include static characters in an inner layout

When using NLog to write to a file target, I want my layout to include " [xxx mS]" if event-properties:item=dur is non-null. Otherwise, nothing will be appended. How can I add the "[" and " mS]" to the when's inner text below? Also, should I use something other than length() in the when's condition for non-null detection?

${message}${when:when='length(${event-properties:item=dur})'>0:inner=${event-properties:item=dur}"

If dur is 437, the log output would be...

<message> [437 mS]

If dur is not set, the log output would be...

<message>

Thoughts?

Upvotes: 2

Views: 342

Answers (2)

Tony
Tony

Reputation: 2066

A bit more trial-n-error led me to this solution...

${message}${when:when='${event-properties:item=dur}'!='':inner= [${event-properties:item=dur} mS]}

It seems you don't have to escape, or quote, the white space and/or square brackets in the 'inner' layout. Whatever appears after 'inner=' is placed into the output.

Edit: Changed conditional (replaced length(...)>0) based on Rolf Kristensen comments.

Upvotes: 2

Rolf Kristensen
Rolf Kristensen

Reputation: 19867

Maybe something like this:

<nlog>
   <variables>
     <variable name="durationMs" value="${when::when='${event-properties:item=dur}'=='':else= \[${event-properties:dur} mS\]" />
     <variable name="defaultLayout" value="${message}${durationMs}" />
   </variables>
</nlog>

Upvotes: 1

Related Questions