/******************************************************************
* 创建人:HTL
* 创建时间:2015-06-03 19:54:49
* 说明: 获取出错时的堆栈调用方法列表
* Huangyuan413026@163.com
*******************************************************************/
using System;
public class StackTraceTest
{
public static void Main()
{
m1();
}
static void m1(){
m2();
}
static void m2(){
m3();
}
static void m3(){
ResponseWrite();
}
static void ResponseWrite(){
ResponseWriteError();
}
static void ResponseWriteError(){
//将错误信息写入日志
Console.WriteLine(GetStackTraceModelName());
}
/// <summary>
/// @Author: HTL
/// @Email: Huangyuan413026@163.com
/// @DateTime: 2015-06-03 19:54:49
/// @Description: 获取当然堆栈的调用方法列表
/// </summary>
/// <returns></returns>
static string GetStackTraceModelName()
{
//当前堆栈信息
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
System.Diagnostics.StackFrame[] sfs = st.GetFrames();
//过虑的方法名称,以下方法将不会出现在最终的方法列表中
string _filterdName = "ResponseWrite,ResponseWriteError,";
string _fullName = string.Empty, _methodName = string.Empty;
for (int i = 1; i < sfs.Length; ++i)
{
if (System.Diagnostics.StackFrame.OFFSET_UNKNOWN == sfs[i].GetILOffset()) break;
_methodName = sfs[i].GetMethod().Name;
if (_filterdName.Contains(_methodName)) continue;
_fullName = _methodName + "()->" + _fullName;
}
st = null;
sfs = null;
_filterdName = _methodName = null;
return _fullName.TrimEnd('-','>');
}
}