Dumping the call stack programmatically

前端 未结 4 549
清酒与你
清酒与你 2021-02-03 19:25

Looking for a way to programmatically dump the call stack and a .net Win Forms app when ever a section of code is hit. Its something I haven\'t come across before but will save

4条回答
  •  离开以前
    2021-02-03 19:47

    http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx

    From MSDN:

    using System.Diagnostics;
    
            StackTrace st = new StackTrace(true);
            for(int i =0; i< st.FrameCount; i++ )
            {
                // Note that high up the call stack, there is only
                // one stack frame.
                StackFrame sf = st.GetFrame(i);
                Console.WriteLine();
                Console.WriteLine("High up the call stack, Method: {0}",
                    sf.GetMethod());
    
                Console.WriteLine("High up the call stack, Line Number: {0}",
                    sf.GetFileLineNumber());
            }
    

提交回复
热议问题