StackTrace.GetFrames()仅返回最后一帧

时间:2017-07-23 12:02:19

标签: c# .net exception mono

我有这段代码

class MainClass
{
    public static void Main(string[] args)
    {

        MiddleMethod();
    }


    public static void MiddleMethod() {
        try
        {
            int b = 0;
            int a = 10 / b;
        } catch (Exception e)
        {
            Console.WriteLine(GetAllFootprints(e));
        }
    }


    public static string GetAllFootprints(Exception x)
    {
        var st = new StackTrace(x, false);
        var frames = st.GetFrames();
        var traceString = "";
        foreach (var frame in frames)
        {

            traceString += frame.GetMethod().DeclaringType.FullName + "@" + frame.GetMethod().Name;
            traceString += "\n";

        }

        return traceString;
    }
}

当我运行此代码时,我只获得发生异常的最后一个方法。像这样

TestException2.MainClass@MiddleMethod

但我希望在此示例Main方法中也有调用方法。我想要一种方法来打印从开始到异常发生的所有调用方法。我该如何解决这个问题?

0 个答案:

没有答案