Loading ...

ASP.NET: Get Stack trace in C# | CodeAsp.Net

ASP.NET: Get Stack trace in C#

 /5
0 (0votes)

Today I was in a need where I had to get stack trace inside a method. I was not throwing any exception in that method an therefore I was not having any exception object from where I could get the stack trace so I used the following method which will give the stack trace in string:

  public static string StackTraceToString() 
{
var sb = new StringBuilder(1000000);
var frames = new StackTrace().GetFrames();
if (frames != null)
{
for (int i = 1; i < frames.Length; i++)
{
var currFrame = frames[i];
var method = currFrame.GetMethod();
sb.Append(string.Format("{0}:{1}", method.ReflectedType.Name,
method.Name)).AppendLine();
}
}
return sb.ToString();

Comments (no comments yet)

Top Posts