C#使用StackTrace擷取方法被誰調用

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   使用   io   for   ar   

在方法中扔進這段

System.Diagnostics.Debug.WriteLine(new string(‘*‘, 78));System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();System.Diagnostics.StackFrame[] sfs = st.GetFrames();for (int u = 0; u < sfs.Length; ++u){   System.Reflection.MethodBase mb = sfs[u].GetMethod();    System.Diagnostics.Debug.WriteLine("[CALL STACK][{0}]: {1}.{2}", u, mb.DeclaringType.FullName, mb.Name);}

然後就能在output視窗(Alt+2)看到類似

******************************************************************************[CALL STACK][0]: WindowsFormsApplication1.Form1.button1_Click[CALL STACK][1]: System.Windows.Forms.Button.OnMouseUp[CALL STACK][2]: System.Windows.Forms.Control.WmMouseUp[CALL STACK][3]: System.Windows.Forms.Control.WndProc[CALL STACK][4]: System.Windows.Forms.ButtonBase.WndProc[CALL STACK][5]: System.Windows.Forms.Button.WndProc[CALL STACK][6]: System.Windows.Forms.NativeWindow.DebuggableCallback[CALL STACK][7]: System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW[CALL STACK][8]: System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW[CALL STACK][9]: System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop[CALL STACK][10]: System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner[CALL STACK][11]: System.Windows.Forms.Application+ThreadContext.RunMessageLoop[CALL STACK][12]: WindowsFormsApplication1.Program.Main[CALL STACK][13]: System.AppDomain._nExecuteAssembly[CALL STACK][14]: Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly[CALL STACK][15]: System.Threading.ExecutionContext.RunInternal[CALL STACK][16]: System.Threading.ExecutionContext.Run[CALL STACK][17]: System.Threading.ExecutionContext.Run[CALL STACK][18]: System.Threading.ThreadHelper.ThreadStart

再高(dan)級(teng)一點 ,寫個公用的靜態方法,並用上“DEBUG”條件之,然後……在需要的地方調用之

[System.Diagnostics.Conditional("DEBUG")]static public void OutputCallStack(){    System.Diagnostics.Debug.WriteLine(new string(‘*‘, 78));    System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();    System.Diagnostics.StackFrame[] sfs = st.GetFrames();    for (int u = 1; u < sfs.Length; ++u)    {System.Reflection.MethodBase mb = sfs[u].GetMethod();System.Diagnostics.Debug.WriteLine("[CALL STACK][{0}]: {1}.{2}", u, mb.DeclaringType.FullName, mb.Name);    }}

思考題:能否利用擴充方法擴充到Debug命名空間下呢?

另:西加加得到呼叫堆疊的東東都在execinfo.h 點擊乘坐直達列車

 

C#使用StackTrace擷取方法被誰調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.