Is there a way in Nlog to output certain character only if Exception is not null. For example my layout is:
layout="${longdate}|${callsite:skipFrames=1}|${message}|${exception:format=tostring}"
If I call NLog.Debug("Hello")
the output will be:
2015-12-07 11:50:00.5114|MyDll.MyClass.MyMethod|Hello|
That last character |
is being printed out. Is there a way to prevent this, and only print it out if there is an actual exception being printed?
You can use the ${onexception:INNER}
layout renderer for this.
${message}${onexception:|${exception:format=Type,Message,StackTrace,Data}}
If there is an exception, it will prepend a '|' followed by whatever you specifiy as your exception format. If no exception is present, only the ${message} will be rendered.
Also look at "When" Layout Renderer
${when:when=Condition:inner=Layout}
EDIT by OP to show working solution for future visitors:
layout="${longdate}|${callsite:skipFrames=1}|${message}${when:when=length('${exception}')>0:Inner=|}${exception:format=tostring}"
I've been using the exceptionSeparator
parameter of $(message)
, this is only output if there is an exception. Eg. to give a space between message an exception:
<variable name="StdLayout"
value="${longdate} | ${level} | ${logger} | ${message:exceptionSeparator= }${exception:format=tostring}" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With