Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Stack trace when logging exceptions with NLog?

When I use the default layout with NLog it only prints the name of the exception. I've been told that the log4jxmlevent layout doesn't prints nothing about the exception. What layout will help me?

Example code:

try {     throw new SystemException(); } catch (Exception ex) {     logger.Error("oi", ex); } 

Default layout output:

2011-01-14 09:14:48.0343|ERROR|ConsoleApplication.Program|oi 

log4jxmlevent output:

<log4j:event logger="ConsoleApplication.Program"            level="ERROR"            timestamp="1295003776872"            thread="9"> <log4j:message>oi</log4j:message> <log4j:NDC /> <log4j:locationInfo class="ConsoleApplication.Program"                     method="Void Main(System.String[])"                     file="C:\Users\User\Documents\Visual Studio 2010\Projects\ConsoleApplication\ConsoleApplication\Program.cs"                     line="21" /> <nlog:eventSequenceNumber>3</nlog:eventSequenceNumber> <nlog:locationInfo assembly="ConsoleApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /> <log4j:properties>   <log4j:data name="log4japp"               value="true" />   <log4j:data name="log4jmachinename"               value="MACHINE" /> </log4j:properties> 

like image 285
Jader Dias Avatar asked Jan 13 '11 19:01

Jader Dias


People also ask

What is stack trace in exception?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

How do I print a stack trace log file?

To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.

Should stack trace be logged?

Therefore, you should log a stacktrace if, and only if, and always if, the exception indicates a bug in the program. However, that does not always indicate that a method you write should catch and log the exception.


2 Answers

I had to use the one of the Logger. + Level + Exception methods:

logger.ErrorException("ex", ex); 

and a custom layout

layout="${exception:format=ToString,StackTrace}${newline}" 
like image 143
Jader Dias Avatar answered Oct 01 '22 21:10

Jader Dias


As documented in How to Log Exceptions, starting with NLog 4.0, pass the exception as the first parameter to Error, for example like this:

logger.Error(ex, "Nickers!"); 

In the NLog configuration (e.g. in web.config or app.config), include ${exception:format=tostring} in the layout, for example like this:

<target name="f" type="File" layout="${longdate} ${message} ${exception:format=tostring}"/>  
like image 24
Edward Brey Avatar answered Oct 01 '22 20:10

Edward Brey