当使用 NLog 写入文件目标时,如果 event-properties:item=dur 为非空,我希望我的布局包含“[xxx mS]”。否则,不会添加任何内容。如何将“[”和“mS]”添加到下面的时间内部文本中?另外,我应该在when的条件中使用length()以外的东西来进行非空检测吗?
${message}${when:when='length(${event-properties:item=dur})'>0:inner=${event-properties:item=dur}"
如果 dur 为 437,则日志输出将为...
<message> [437 mS]
如果未设置dur,日志输出将为...
<message>
想法?
最佳答案
更多的尝试和错误让我找到了这个解决方案......
${message}${when:when='${event-properties:item=dur}'!='':inner= [${event-properties:item=dur} mS]}
看来您不必转义或引用“内部”布局中的空格和/或方括号。 'inner=' 之后出现的任何内容都会放入输出中。
编辑:根据 Rolf Kristensen 评论更改了条件(替换 length(...)>0)。
关于c# - NLog:如何在内部布局中包含静态字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59825887/