fork(4) download
  1. /******************************************************************
  2.  * 创建人:HTL
  3.  * 创建时间:2015-06-03 19:54:49
  4.  * 说明: 获取出错时的堆栈调用方法列表
  5.  * Huangyuan413026@163.com
  6.  *******************************************************************/
  7. using System;
  8.  
  9. public class StackTraceTest
  10. {
  11. public static void Main()
  12. {
  13. m1();
  14. }
  15. static void m1(){
  16. m2();
  17. }
  18.  
  19. static void m2(){
  20. m3();
  21. }
  22.  
  23. static void m3(){
  24. ResponseWrite();
  25. }
  26. static void ResponseWrite(){
  27. ResponseWriteError();
  28. }
  29. static void ResponseWriteError(){
  30. //将错误信息写入日志
  31. Console.WriteLine(GetStackTraceModelName());
  32. }
  33. /// <summary>
  34. /// @Author: HTL
  35. /// @Email: Huangyuan413026@163.com
  36. /// @DateTime: 2015-06-03 19:54:49
  37. /// @Description: 获取当然堆栈的调用方法列表
  38. /// </summary>
  39. /// <returns></returns>
  40. static string GetStackTraceModelName()
  41. {
  42. //当前堆栈信息
  43. System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
  44. System.Diagnostics.StackFrame[] sfs = st.GetFrames();
  45. //过虑的方法名称,以下方法将不会出现在最终的方法列表中
  46. string _filterdName = "ResponseWrite,ResponseWriteError,";
  47. string _fullName = string.Empty, _methodName = string.Empty;
  48. for (int i = 1; i < sfs.Length; ++i)
  49. {
  50. if (System.Diagnostics.StackFrame.OFFSET_UNKNOWN == sfs[i].GetILOffset()) break;
  51. _methodName = sfs[i].GetMethod().Name;
  52. if (_filterdName.Contains(_methodName)) continue;
  53. _fullName = _methodName + "()->" + _fullName;
  54. }
  55. st = null;
  56. sfs = null;
  57. _filterdName = _methodName = null;
  58. return _fullName.TrimEnd('-','>');
  59. }
  60. }
Success #stdin #stdout 0.03s 24208KB
stdin
Standard input is empty
stdout
Main()->m1()->m2()->m3()