How to log stack trace with log4net
? I am using .Net
version.
They way I have is Log.Error(ex)
.
Thanks
There are two basic forms, one that takes an object and an exception explicitly:
catch(Exception ex)
{
// the form that takes two args has an exception as second, prints trace...
_log.Error("My custom message", ex);
}
And one that takes any object and performs a ToString()
on it:
catch(Exception ex)
{
// the form that takes one arg uses ToString()
_log.Error(ex);
}
The former allows you to attach a more meaningful message on the log entry first to give any additional detail you'd like. The latter will do the job but only prints the exception details using ToString()
, which gives you:
The default implementation of ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is Nothing, its value is not included in the returned string.