【问题标题】:NLog: How to include static characters in an inner layoutNLog:如何在内部布局中包含静态字符
【发布时间】:2020-01-22 21:27:29
【问题描述】:

当使用 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>

想法?

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

    更多的反复试验导致我找到了这个解决方案......

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

    您似乎不必在“内部”布局中转义或引用空格和/或方括号。将 'inner=' 之后出现的任何内容放入输出中。

    编辑:根据 Rolf Kristensen cmets 更改条件(替换长度(...)>0)。

    【讨论】:

    • 认为这更快${when::when='${event-properties:item=dur}'=='':else= [${event-properties:dur} mS]。对length('') 的调用有额外的开销。
    • 完美,这就是我想知道的。我已经更新了我的答案(上图)。尽管我在代码中使用了上面显示的格式,但我会将您的建议标记为答案。
    【解决方案2】:

    可能是这样的:

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

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 2021-07-29
      • 1970-01-01
      相关资源
      最近更新 更多