Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Praise to Reflector, I found a nice StackTraceToString() method

I must give my praise to the Reflector program, made by Lutz Roeder. I used it to dig down into the depths of TraceListener, to see how the config sections was read and how the DefaultTraceListener wrote it's stacktrace to the file - StackTraceToString(). Saved me some time, it did ;)

Here's my refactored version:

            /// <summary>

            /// Converts a stacktrace into something more readable

            /// </summary>

            /// <param name="trace">the stacktrace to handle</param>

            /// <returns>a string with the stacktrace</returns>

            public static string StackTraceToString(StackTrace trace)

            {

                  int startFrameIndex = 0;

                  int endFrameIndex = trace.FrameCount - 1;

                  int iFrame;

                  int iParam;

                  int iFileLineNumber;

 

                  StringBuilder sbReturnString;

                  StackFrame frame;

                  MethodBase mBase;

                  ParameterInfo[] arrParamInfo;

                  ParameterInfo paramInfo;

                  sbReturnString = new StringBuilder(512);

                  for (iFrame = startFrameIndex; (iFrame <= endFrameIndex); iFrame = (iFrame + 1))

                  {

                        frame = trace.GetFrame(iFrame);

                        mBase = frame.GetMethod();

                        sbReturnString.Append(" at ");

                        sbReturnString.Append(mBase.ReflectedType.Name);

                        sbReturnString.Append(".");

                        sbReturnString.Append(mBase.Name);

                        sbReturnString.Append("(");

                        arrParamInfo = mBase.GetParameters();

                        for (iParam = 0; (iParam < arrParamInfo.Length); iParam = (iParam + 1))

                        {

                              paramInfo = arrParamInfo[iParam];

                              if (iParam > 0)

                              {

                                    sbReturnString.Append(", ");

 

                              }

                              sbReturnString.Append(paramInfo.ParameterType.Name);

                              sbReturnString.Append(" ");

                              sbReturnString.Append(paramInfo.Name);

 

                        }

                        sbReturnString.Append(") ");

                        sbReturnString.Append(frame.GetFileName());

                        iFileLineNumber = frame.GetFileLineNumber();

                        if (iFileLineNumber > 0)

                        {

                              sbReturnString.Append("(");

                              sbReturnString.Append(iFileLineNumber.ToString());

                              sbReturnString.Append(")");

 

                        }

                        sbReturnString.Append(Environment.NewLine);

                  }

                  sbReturnString.Append(" ");

                  return sbReturnString.ToString();

            }

      }

 

Thank you, thank you, thank you Lutz :)

No Comments