views:

218

answers:

3

How do I log inner exception with Log4NET?

This is my current conversion pattern:

<conversionPattern value="%date [%appdomain] %-5level %logger [%property{NDC}] - %message%newline" />
A: 

Console and File Appenders automatically print the exception. The exception: message, stack trace, and all inner exceptions (again with stack trace) are logged on separate lines and do not follow the conversion pattern.

I am not even sure if you could configure log4net not to print it.

Stefan Egli
Hi Stefan, Can you point me to a relevant entry in SDK confirming what you answered?
Raj
I did not read this anywhere. This is what I see in my log files or on the console when I use log4net. Inner exceptions are always printed.
Stefan Egli
A: 

After some quick googling, I found someone else who had a similar problem. Freddy Gómez's solution was:

You can implement a Renderer (a class which implements the interface IObjectRenderer) for your exception and put the information you want about the exception. Then, you just have to add a element to the config file.

John Nelson
The other solutions in the same thread I referenced included parsing the exception and including extra information in the Message itself. <http://old.nabble.com/RE:-Excluding-stack-trace-from-Layout-p18737810.html>
John Nelson
A: 
%exception 

a formatted form of the exception object in the log entry, if the entry contains an exception; otherwise, this format expression adds nothing to the log entry

Reference: http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx

I believe your exception would contain the inner exception:

Edit: use the ILog.Error() method instead of ILog.ErrorFormat(). As per documentation, ErrorFormat() does not take an Exception object to include in the log event

ram