Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to log stack trace with log4net? I am using .Net version.

They way I have is Log.Error(ex).

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
877 views
Welcome To Ask or Share your Answers For Others

1 Answer

Use this:

void Error(object message,Exception t)

Reason is in log4net documentation for void Error(object message):

WARNING Note that passing an Exception to this method will print the name of the Exception but no stack trace. To print a stack trace use the void Error(object,Exception) form instead.

Error(object message, Exception t) is the most flexible way to log exception data because it goes as an Exception rather than Object and that can be used in appenders to narrow logs to a particular exception class (rather than by searching for a string which is much slower and less consistent)

There are special versions of all non-format logging methods that take message and exception:

namespace log4net
{
    public interface ILog
    {
        ...
        /* Log a message object and exception */
        void Debug(object message, Exception t);
        void Info(object message, Exception t);
        void Warn(object message, Exception t);
        void Error(object message, Exception t);
        void Fatal(object message, Exception t);
        ...
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...