13

Target:

<targets>
    <target name="file" xsi:type="File" layout="${longdate} ${level} ${message} ${exception}" fileName="${basedir}/log.txt" archiveAboveSize="10485760" />
</targets>

When I call Logger.Error("some message", e), where e is some exception object, it only logs the message, not the exception information. I need it to output exception message and stack trace. Any ideas what I am doing wrong?

Andrey
  • 20,487
  • 26
  • 108
  • 176

3 Answers3

22

Try to use ${exception:innerFormat=Message,StackTrace}. Here is the documentation.

Alnoor
  • 72
  • 8
kolbasov
  • 1,548
  • 11
  • 15
11

I've found ${exception:format=tostring} to be the best format for logging full details.

NLog - How to Log Exceptions

Taran
  • 12,822
  • 3
  • 43
  • 47
  • 1
    For NLog >v5.0 you can use `${message:withexception=true}`, [see here](https://github.com/nlog/NLog/wiki/How-to-log-exceptions#exception-formatting). – FredM Mar 23 '23 at 13:33
0

Yep try adding

includeSourceInfo="true"

to your target file, like;

 <target name="viewer"
            xsi:type="NLogViewer"
            includeSourceInfo="true"
            address="udp://127.0.0.1:9999" />
keithl8041
  • 2,383
  • 20
  • 27