如何在NLog布局模式中使某个块可选? [英] How to make a certain piece optional in a NLog layout pattern?

查看:77
本文介绍了如何在NLog布局模式中使某个块可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 job 的自定义布局渲染器.它提供了几项,在我们的app.config中像这样使用:

I have a custom layout renderer named job. It provides several items, which are used like this in our app.config:

<variable name="jobHost" value = "${job:item=host}" />
<variable name="jobService" value = "${job:item=service}" />
<variable name="jobNS" value = "${job:item=ns}" />
<variable name="jobName" value = "${job:item=name}" />
<variable name="jobKind" value = "${job:item=kind}" />
<variable name="jobScheduleId" value = "${job:item=scheduleId}" />
<variable name="jobLayout" value = "[H:${jobHost} S:${jobService} NS:${jobNS} N:'${jobName}' K:${jobKind} S:${jobScheduleId}]" />
<variable name="layout" value = "${longdate} [${threadid}] ${machinename} ${jobLayout} ${uppercase:inner=${level}} ${logger} - ${message} ${onexception:${newline}${exception:format=ToString:innerFormat=ToString:maxInnerExceptionLevel=2}}" />
<targets>
    <target name="ThreadLog" xsi:type="ThreadSpecificTarget" />
    <target xsi:type="SplitGroup" name="AllTargets">
        <target name="TextFile" xsi:type="File" fileName="C:\Log\QuartzBackgroundEngine.txt" layout="${layout}"/>
        <target name="Console" xsi:type="ColoredConsole" layout="${layout}"/>
    </target>
</targets>

当布局渲染器确定没有可用的特定于作业的数据时,日志消息将包含字符串[H: S: NS: N:'' K: S:].我要排除它.

When the layout renderer decides that there is no job specific data available, then log messages contain the string [H: S: NS: N:'' K: S:]. I want to exclude it.

因此,我尝试用${jobLayout:when=jobName!=’’}替换${jobLayout},但是它不起作用,因为NLog认为jobLayout应该对应于布局渲染器,而不是变量,在这种情况下.

So, I tried to replace ${jobLayout} with ${jobLayout:when=jobName!=’’}, but it does not work, because NLog thinks that jobLayout should correspond to a layout renderer, rather than a variable, which is the case here.

如何更改布局,以便仅在${jobName}不为空的情况下包含${jobLayout}?

How can I change my layout so that ${jobLayout} is only included if ${jobName} is not empty?

推荐答案

我遇到了类似的问题,当变量不为空时,我想显示一些特殊的格式.

I ran into a similar problem where I wanted to make to display some special formatting when a variable was not empty.

我从最有用的NLog配置

${when:when=length('${jobName}') > 0:inner=${jobLayout}}

${when:when=length('${jobName}') > 0:inner=${jobLayout}}

我不确定这是否能使您100%达到目标,但这是朝正确方向迈出的一步.

I'm not sure if this will get you 100% there, but it is a step in the right direction.

这篇关于如何在NLog布局模式中使某个块可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆