Prevent NLog from rendering inner text when layout

2020-07-18 05:05发布

问题:

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

回答1:

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



回答2:

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



标签: nlog