16

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?

Bagzli
  • 6,254
  • 17
  • 80
  • 163

5 Answers5

19

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}"
Bagzli
  • 6,254
  • 17
  • 80
  • 163
skalinkin
  • 1,024
  • 9
  • 19
15

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.

Alex
  • 7,901
  • 1
  • 41
  • 56
3

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}" />
Richard
  • 106,783
  • 21
  • 203
  • 265
  • so instead of a space I can put `|` after the `=` in the message clause? This will cause the `|` to be printed out only if there is an exception? – Bagzli Dec 07 '15 at 17:17
  • I tried that with `layout="${longdate}|${callsite:skipFrames=1}|${message:exceptionSeparator=|}${exception:format=tostring}"` and it didn't print it out. I also tried it with a space and it still did not work. When exception is there, it does not get printed out. – Bagzli Dec 07 '15 at 17:23
  • My first check would be on nLog versions... but I've currently only got limited access to the project containing the above configuration. – Richard Dec 07 '15 at 17:33
2

I will Combine two above answers

  • using when layout render like @skalinkin answer But I think preferred use exception's message for detect exception if null or not, Like this
layout="${longdate}|${message}${when:when=length('${exception:format=tostring}')>0:Inner=|}${exception:format=tostring}"
  • using onexception layout render like @Alex answer
layout="${longdate}|${message}${onexception:inner=|Exception\: ${exception:format=tostring}}"

For More Details about suggested layouts Here Official Doc

I Preferred to use the second Suggest

ahmed hamdy
  • 5,096
  • 1
  • 47
  • 58
1

You can define a target which explicitly test if exception is not null :

<target name="fileAsException"
        xsi:type="FilteringWrapper"
        condition="length('${exception}')>0">
  <target xsi:type="File"
          fileName="c:\my path\exceptions.log"
          layout="${ExceptionVerboseLayout}" />
</target>

(See the "condition="length('${exception}')>0">" line)
You can bind it with a specific layout ("ExceptionVerboseLayout" in my example).

AFract
  • 8,868
  • 6
  • 48
  • 70
  • I'm having troubles understanding how this works, would you be able to give me an example based on my question above? What is confusing me is that you are putting a target inside a target, how does it know which message to show? I still need Regular message to be displayed. – Bagzli Dec 07 '15 at 17:12
  • I answer you even if you already found your solution :). The syntax is a little bit confusing. It's a single target, not one included in another. To display regular messages along with exceptions you can use skalinkin answer, it will be simpler. – AFract Dec 08 '15 at 08:33