NLog LogException seems to ignore the exception
Asked Answered
V

2

36

LogException or any of the derived functions like ErrorException etc. seem to totally ignore the exception parameter passed in.

Am I missing a format attribute in my nlog.config file? I am using the boilerplate from the template that Nlog installs in VS.

I would expect information from the exception object AND inner exceptions to be added to the log file. Yet the only information added to the log file is the string parameter passed to the function.

Turns out that ErrorException() is actually less useful than Error()

How can I get more in depth reporting. Particularly a full recursive dump of the Message property of all inner Exceptions?

Vershen answered 6/4, 2011 at 12:54 Comment(0)
M
51

Add or replace the ${exception} tag in the layout config to ${exception:format=tostring}

 <targets>
    <target name="errorLogFile" xsi:type="File" fileName="errors.txt" 
            layout="${message} ${exception:format=tostring}"/>
</targets>
Malchus answered 6/4, 2011 at 13:6 Comment(1)
Any ideas on how to do this programatically?Singultus
C
32

Building on @Niki's answer, this will give you pretty close to what log4net will do

fileName="${basedir}/logs/${shortdate}.log" layout="${longdate}|${level:uppercase=true}|${logger}|${message}${onexception:inner=${newline}${exception:format=tostring}}"
Chappie answered 3/11, 2011 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.