Nlog output characters if exception not null

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

Tags:

C#

Asp.Net

Nlog