使用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
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
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);
}
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);
}
}
}