是否log4net的支持,包括在日志信息的调用堆栈 [英] Does log4net support including the call stack in a log message

查看:808
本文介绍了是否log4net的支持,包括在日志信息的调用堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望包括调用堆栈在log4net的消息(如调用我的方法)。是否有这样做的标准方式?

(我知道这将是缓慢的,但我只需要做的一些错误)

解决方案

是 - 您可以通过以下方式中的图案布局得到这个堆栈信息:

 %类型%文件%行%方法%位置%类
 

有关详细信息,请参见上的PatternLayout这个的文档。

修改响应低于伊恩的评论:我不的认为的log4net的可配置写出整个堆栈

您可以随时退回上写了就知道,使用类似新的堆栈跟踪()的ToString(),但我猜你问的原因是你想这是在日志记录配置进行配置。

我将有一个更深入的了解,但我的直觉是没有办法配置这一点,那你最终不得不实现自己的布局类。

修改++ 确定 - 这里是源于的PatternLayout 但增加了一个布局%堆在一个自定义模式布局类

这code是有点粗糙 - 只说明 - 不是生产准备好了! (例如,您可能没有安全权限来访问您要打印的堆栈)

 公共类CustomPatternLayout:的PatternLayout
{
    公共CustomPatternLayout()
    {
        this.AddConverter(堆栈中的typeof(StackTraceConverter));
    }
}

公共类StackTraceConverter:PatternLayoutConverter
{
    保护覆盖无效转换(TextWriter的作家,LoggingEvent所LoggingEvent所)
    {
        VAR堆栈=新的堆栈跟踪();

        变种帧= stack.GetFrames();
        对于(VAR I = 0; I< frames.Length;我++)
        {
            VAR帧=帧[I]

            //如果堆栈帧对应仍在log4net的组件内,跳过它。
            如果(frame.GetMethod()。DeclaringType.Assembly!= typeof运算(日志管理).Assembly)
            {
                writer.WriteLine({0} {1}行{2},
                    frame.GetMethod()。DeclaringType.FullName,
                    frame.GetMethod()。名称,
                    frame.GetFileLineNumber());
            }
        }
    }
}
 

然后,您可以(在布局的最后音符%堆栈)配置用下面的方式配置

 <布局类型=ScratchPad.CustomPatternLayout,暂存器>
    < conversionPattern值=%DATE%-5level%消息%换行符%类型%文件%行%方法%位置%类别%堆栈/>
  < /布局>
 

I wish to include the call stack (e.g. the methods that called me) in a log4net message. Is there a standard way of doing this?

(I know this will be slow, but I only need to do it on some errors)

解决方案

Yes - you can get this stack information using the following patterns in a pattern layout:

%type %file %line %method %location %class

See the this documentation on the PatternLayout for more information.

Edit in response to Ian's comment below: I don't think log4net can be configured to write out the whole stack.

You can always fall back on writing it out for yourself, using something like new StackTrace().ToString(), but I'm guessing the reason you ask is you want this to be configurable in the logging configuration.

I'll have a deeper look, but my gut feeling is there is no way to configure this, and that you'd end up having to implement your own Layout class.

Edit++ OK - here is a custom pattern layout class that derives from PatternLayout but adds in a layout %stack.

This code is a bit rough - illustrative only - not production ready! (for example, you may not have security permission to access the stack you are trying to print)

public class CustomPatternLayout : PatternLayout
{
    public CustomPatternLayout()
    {
        this.AddConverter("stack", typeof(StackTraceConverter));
    }
}

public class StackTraceConverter : PatternLayoutConverter
{
    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var stack = new StackTrace();

        var frames = stack.GetFrames();
        for (var i = 0; i < frames.Length; i++ )
        {
            var frame = frames[i];

            // if the stack frame corresponds to still being inside the log4net assembly, skip it.
            if (frame.GetMethod().DeclaringType.Assembly != typeof(LogManager).Assembly)
            {
                writer.WriteLine("{0}.{1} line {2}",
                    frame.GetMethod().DeclaringType.FullName,
                    frame.GetMethod().Name, 
                    frame.GetFileLineNumber());
            }
        }
    }
}

You can then configure this with the following pattern configuration (note %stack at the end of the layout):

  <layout type="ScratchPad.CustomPatternLayout,ScratchPad">
    <conversionPattern value="%date %-5level %message%newline %type %file %line %method %location %class %stack" />
  </layout>

这篇关于是否log4net的支持,包括在日志信息的调用堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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