log4Net 的问题

闲谈李 2008-01-30 12:06:15
使用Log4net框架的时候:

ILog log = LogManager.GetLogger(this.GetType());
log.Debug("This is a Test");

然后在配置文件中配置 %M或者%method 就可以记录 出现问题的方法名称。
有谁分析过log4Net的源代码,这个method的名字是怎么获取的呢?

我跟踪了一下。
protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
{
writer.Write(loggingEvent.LocationInformation.MethodName);
}
然后继续是

LocationInfo这个类,关键的代码是:
StackTrace trace = new StackTrace(true);
int index = 0;
while (index < trace.FrameCount)
{
StackFrame frame = trace.GetFrame(index);
if ((frame != null) && (frame.GetMethod().DeclaringType == callerStackBoundaryDeclaringType))
{
break;
}
index++;
}
while (index < trace.FrameCount)
{
StackFrame frame2 = trace.GetFrame(index);
if ((frame2 != null) && (frame2.GetMethod().DeclaringType != callerStackBoundaryDeclaringType))
{
break;
}
index++;
}
if (index < trace.FrameCount)
{
StackFrame frame3 = trace.GetFrame(index);
if (frame3 != null)
{
MethodBase method = frame3.GetMethod();
if (method != null)
{
this.m_methodName = method.Name;
if (method.DeclaringType != null)
{
this.m_className = method.DeclaringType.FullName;
}
}
this.m_fileName = frame3.GetFileName();
this.m_lineNumber = frame3.GetFileLineNumber().ToString(NumberFormatInfo.InvariantInfo);
this.m_fullInfo = string.Concat(new object[] { this.m_className, '.', this.m_methodName, '(', this.m_fileName, ':', this.m_lineNumber, ')' });


就是this.m_methodName ,但是我拷贝出来以后,得到的结果是不一样的。高手们帮忙看一下。

其中GetInfo是我自己写的代码

private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(MyLog.GetInfo(this.GetType()));
}
我本来想得到 button_Click 这个字符串,结果得到的是OnClick
...全文
90 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
海渊 2009-08-11
  • 打赏
  • 举报
回复
LogManager.GetLogger(MethodInfo.GetCurrentMethod().DeclaringType)请教一下这句代码的意思?谢谢!
闲谈李 2008-02-22
  • 打赏
  • 举报
回复
问题我自己解决了,不过还是谢谢大家的帮忙!!
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using log4net;
namespace myLogTest
{
public class LogMgr
{
private static ILog log = LogManager.GetLogger(MethodInfo.GetCurrentMethod().DeclaringType);

private static string LogPosition()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
return "Position:" + sf.GetMethod().DeclaringType.Name+"["+sf.GetMethod().Name+"]\r\n";
}

public static void Error(string message)
{
log.Error(LogPosition()+message);
}
public static void Debug(string message)
{
log.Debug(LogPosition()+ message);

}
public static void Info(string message)
{
log.Info(message);
}
public static void Warn(string message)
{
log.Warn(message);
}
}
}
lextm 2008-01-30
  • 打赏
  • 举报
回复
看了lz的代码,估计是你没有很好的理解StackFrame,所以你简单的copy别人的代码出错可以理解。你需要的层数和别人使用的层数是不一样的,所以你拿到的不是你想要的东西。

楼上的改法就是正解。
机器人 2008-01-30
  • 打赏
  • 举报
回复
http://blog.csdn.net/fangxinggood/archive/2007/02/25/1514291.aspx

其实关键是:

Dim st As StackTrace = New StackTrace()
Dim sf As StackFrame = st.GetFrame(2)

这个 2 表示从该代码开始被调用的层次。

你用下面代码:Message出来的就是“button4_Click”
  private   void   button4_Click(object   sender,   EventArgs   e) 
{
StatckTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
MessageBox.Show(sf.GetMethod().DeclaringType.Name);
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧