Skip to content

Instantly share code, notes, and snippets.

@oguzhankircali
Last active March 27, 2019 19:09
Show Gist options
  • Save oguzhankircali/10e345aee33f62b4e5001d9c42baa6b5 to your computer and use it in GitHub Desktop.
Save oguzhankircali/10e345aee33f62b4e5001d9c42baa6b5 to your computer and use it in GitHub Desktop.
Get full stack trace with error text, method and line number
public static string GetAllFootprints(Exception x)
{
var st = new StackTrace(x, true);
var frames = st.GetFrames();
var traceString = "";
foreach (var frame in frames)
{
if (frame.GetFileLineNumber() < 1)
continue;
traceString +=
"File: " + frame.GetFileName() +
", Method:" + frame.GetMethod().Name +
", LineNumber: " + frame.GetFileLineNumber();
traceString += " –> ";
}
return traceString;
}
@oguzhankircali
Copy link
Author

Büyük bir otomasyon sistemi ile uğraşıyorum. Hataları Global.asax’da yakalıyorum fakat iç içe çağırılmış 10 metottan hangisinin neresinde hata verdiğini bilmiyorum. Bu durum da çözüme ulaşmamı oldukça zorlaştırıyordu ta ki StackTrace’in varlığını keşfedene kadar. Hata aldığınız noktadan itibaren onun ilk çağırıldığı yerden son çağırıldığı yere kadar metod, satır numarası, class adı tutulan bir objedir StackTrace. Aşağıda hazırladığım metoda Exception’u verirseniz, o da size hatanın tek tek (sınıf, metod, satır numarası) verecektir.

ÖR:
File: UnitFactory.cs, Method: GetUnit, LineNumber: 457 –> File: Controllers\UnitController.cs, Method: Index, LineNumber: 115 –>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment