Represents a stack trace, which is an ordered collection of one or more stack frames.

 static void Main(string[] args)
        {
            A();
            Console.ReadLine();
        }

        static void A()
        {
            B();
        }

        static void B()
        {
            C();
        }

        static void C()
        {
            StackTrace s = new StackTrace(true);
            Console.WriteLine($"Total frames: {s.FrameCount}");
            Console.WriteLine($"Current Method:{s.GetFrame(0).GetMethod().Name}");
            Console.WriteLine($"Calling method:{s.GetFrame(1).GetMethod().Name}");
            Console.WriteLine($"Entry method: {s.GetFrame(s.FrameCount - 1).GetMethod().Name}");
            Console.WriteLine("Call stack: ");
            foreach (StackFrame f in s.GetFrames())
            {
                Console.WriteLine($"File:{f.GetFileName()} Line:{f.GetFileLineNumber()} col:{f.GetFileColumnNumber()}, Offset:{f.GetILOffset()}  Method:{f.GetMethod().Name}");
            }
        }

 

posted @ 2020-06-01 14:40  FredGrit  阅读(111)  评论(0编辑  收藏  举报