如何使用log4net(C#)记录堆栈跟踪 [英] How to log stack trace using log4net (C#)

查看:356
本文介绍了如何使用log4net(C#)记录堆栈跟踪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用log4net记录堆栈跟踪?我正在使用.Net版本.

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

我的方式是Log.Error(ex).

谢谢

推荐答案

使用此:

void Error(object message,Exception t)

原因在void Error(object message)的log4net文档中:

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

警告请注意,将Exception传递给此方法将打印Exception的名称,但不显示堆栈跟踪.要打印堆栈跟踪,请使用void Error(object,Exception)表单.

Error(object message, Exception t)是记录异常数据的最灵活的方法,因为它作为异常而不是对象出现,并且可以在追加程序中用于将日志范围缩小到特定的异常类(而不是通过搜索字符串来查找大量异常)速度较慢且一致性较低)

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);
        ...
    }
}

这篇关于如何使用log4net(C#)记录堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆