NLog:如何在内部布局中包含静态字符

NLog: How to include static characters in an inner layout

当使用 NLog 写入文件目标时,如果 event-properties:item=dur 不为空,我希望我的布局包含“[xxx mS]”。否则,不会附加任何内容。如何将“[”和“mS]”添加到下面 when 的内部文本中?另外,我是否应该在 when 条件下使用 length() 以外的其他东西来进行非空检测?

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

如果 dur 是 437,日志输出将是...

<message> [437 mS]

如果未设置 dur,日志输出将是...

<message>

想法?

可能是这样的:

<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>

更多的试错让我找到了这个解决方案...

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

看来您不必转义或引用 'inner' 布局中的白色 space and/or 方括号。 'inner=' 之后出现的任何内容都将放入输出中。

编辑:根据 Rolf Kristensen 的评论更改了条件(替换为 length(...)>0)。