在NLog中,有没有办法让变量带有条件值?我见过这个:https://github.com/NLog/NLog/wiki/When-Layout-Renderer并尝试了以下内容:
<variable name="EnvironmentString" value="${when:when='${IsProd}' == 'true':Prod:else:Stage}"/>
但该值只是作为文字返回;逻辑没有被处理。
谢谢,
答案 0 :(得分:1)
1)将语法更改为:
${when:when='${var:IsProd}'=='true':inner=Prod:else=Stage}
注意:
:else =而不是:else:
:内=
var:IsProd(假设IsProd是另一个变量)
2)将整个条件移动到最终布局
<target ... layout="other stuff|${when:when='${var:IsProd}'=='true':inner=Prod:else=Stage}|other stuff" ... />
我没有设法让条件工作变量。也许其他人可以说出原因。
答案 1 :(得分:0)
这应该有用。
但这取决于变量的使用方式。
如果您使用:
${EnvironmentString}
然后在加载配置时对其进行评估,因此您可以将其用于所有参数。
要动态评估,请使用
${var:EnvironmentString}
但请注意${var}
仅在目标/布局的参数类型为Layout
时才有效