您的位置:首頁技術文章
文章詳情頁

.NET使用StackTrace獲取方法調用信息的代碼演示

瀏覽:230日期:2022-06-08 11:23:07
目錄
  • 前言
  • 介紹
  • 演示
  • 鏈接

前言

在日常工作中,偶爾需要調查一些詭異的問題,而業務代碼經過長時間的演化,很可能已經變得錯綜復雜,流程、分支眾多,如果能在關鍵方法的日志里添加上調用者的信息,將對定位問題非常有幫助。

介紹

StackTrace, 位于 System.Diagnostics 命名空間下,名字很直觀,它代表一個方法調用的跟蹤堆棧,里面存放著按順序排列的棧幀對象(StackFrame),每當發生一次調用,就會壓入一個棧幀;而一個棧幀,則擁有本次調用的各種信息,除了MethodBase,還包括所在的文件名、行、列等。

演示

下面代碼演示了如何獲取調用者的方法名、所在文件、行號、列號等信息。

public static string GetCaller()
{
       StackTrace st = new StackTrace(skipFrames: 1, fNeedFileInfo: true);
       StackFrame[] sfArray = st.GetFrames();
 
       return string.Join(" -> ",
    sfArray.Select(r =>
$"{r.GetMethod().Name} in {r.GetFileName()} line:{r.GetFileLineNumber()} column:{r.GetFileColumnNumber()}"));
    
}

第一幀是 GetCaller本身,所以跳過;fNeedFileInfo設置成 true,否則調用者所在文件等信息會為空。
簡單創建個控制臺程序并添加幾個類模擬一下,輸出如下:

UpdateOrder in G:\examples\MethodCall2\ClassLevel6.cs line:11 column:8 -> 
Level5Method in G:\examples\MethodCall2\ClassLevel5.cs line:8 column:9 -> 
Level4Method in G:\examples\MethodCall2\ClassLevel4.cs line:10 column:9 -> 
Level3Method in G:\examples\MethodCall2\ClassLevel3.cs line:10 column:9 -> 
Level2Method in G:\examples\MethodCall2\ClassLevel2.cs line:10 column:9 -> 
InternalMethod in G:\examples\MethodCall2\ClassLevel1.cs line:12 column:13 -> 
Main in G:\examples\MethodCall2\Program.cs line:18 column:17

可以看到因為StackTrace是個棧結構(FILO),所以打印出來的順序也是由近及遠的。

鏈接

StackTrace Class https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stacktrace?view=net-6.0

StackFrame Class https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.stackframe?view=net-6.0

到此這篇關于.NET使用StackTrace獲取方法調用信息的文章就介紹到這了,更多相關.NET方法調用信息內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

標簽: ASP.NET
国产综合久久一区二区三区