Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging InnerException using Log4Net

How do I log inner exception with Log4NET?

This is my current conversion pattern:

<conversionPattern value="%date [%appdomain] %-5level %logger [%property{NDC}] - %message%newline" />
like image 776
Raj Avatar asked Mar 09 '10 15:03

Raj


2 Answers

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.

Update: It is possible to configure the appender to not print the stacktrace: Log4Net - Logging out the Exception stacktrace only for certain files

like image 78
Stefan Egli Avatar answered Nov 08 '22 08:11

Stefan Egli


%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

like image 30
ram Avatar answered Nov 08 '22 08:11

ram