Prevent NLog from rendering inner text when layout

2020-07-18 04:48发布

I put the contents of my NLog error email text into a file and render it using a a FileContents renderer. What I'd like to do is hide certain parts of html output if a layout renderer produces an empty string

<div>Request ticket: ${httpContextItems:key=RequestTicket}</div>

I am not sure of the syntax of the when condition/property. Here are some attempts:

${httpContextItems:key=RequestTicket:when:length>0}
   <div>Request ticket: ${httpContextItems:key=RequestTicket}</div>
${??? How to enclose? }

And NLog doesn't like this nested stuff:

${when:when=length(${httpContextItems:key=RequestTicket})=0:inner=<div>Request ticket: ${httpContextItems:key=RequestTicket}</div>}

标签: nlog
2条回答
迷人小祖宗
2楼-- · 2020-07-18 05:01

I faced a similar problem and got things to work like this:

${when:when=length('${httpContextItems:key=RequestTicket}') > 0:inner=&lt;div&gt;Request ticket ${httpContextItems:key=RequestTicket}&lt;/div&gt;}

Note that I had to drop the colon after Request ticket. NLog kept on cutting text before or after the colon, no matter what I tried (escaping with \, used ${literal}, replaced colon with its ASCII code, put the colon in a separate variable...).

查看更多
何必那么认真
3楼-- · 2020-07-18 05:09

And NLog doesn't like this nested stuff:

${when:when=length(${httpContextItems:key=RequestTicket})=0:inner=<div>Request    ticket: ${httpContextItems:key=RequestTicket}</div>}

This was a bug in NLog due to the colon. This should work now since NLog 4.2, so the solution is, upgrade NLog. See this issue on GitHub

查看更多
登录 后发表回答