How to tell NLog to log exceptions?
Asked Answered
P

3

14

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?

Promptbook answered 8/2, 2012 at 18:14 Comment(0)
S
26

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

Sailfish answered 8/2, 2012 at 18:39 Comment(2)
How do you make this work with NLogViewer or Chainsaw targets? I want to use an UDP logger, such as Sentinel or Log2Console.Limey
<target name="udp" xsi:type="Chainsaw" address="udp4://localhost:7071" /> <logger name="*" minlevel="Trace" writeTo="udp" />Beare
E
12

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

NLog - How to Log Exceptions

Ewe answered 17/4, 2015 at 23:3 Comment(1)
For NLog >v5.0 you can use ${message:withexception=true}, see here.Thoughtful
A
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" />
Antoinette answered 16/11, 2015 at 21:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.