[英]Getting BadImageFormatException from other exception's StackTrace
[英]Getting the Stacktrace
當發生異常時,您可以打印出 StackTrace 並查看它。
如果您想在不發生異常的情況下獲取 StackTrace 怎么辦?
有沒有辦法做到這一點?
您可以隨時通過調用Environment.StackTrace打印出堆棧跟蹤
string tracktrace = System.Environment.StackTrace;
當您捕獲異常時,您可以構造StackTrace object 並從中提取有用信息。 請參見以下示例:
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());
}
PS:即使沒有例外,這也有效 - 請參閱How to print the current stack trace in .NET without any exception 。
System.Environment.StackTrace 是一個很棒的工具,但請注意,您並不總能得到您想要的東西,並且 x86 和 x64 平台之間存在差異,這些平台可能會影響 output。 格羅迪細節在這里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.