C# Source Code: Displaying the contents of the Call Stack (using the StackTrace object)
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
C# Source Code
Displaying the contents of the Call Stack (using the StackTrace object)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, April 06, 2004
Hits:
796
Category:
Attributes/Reflection/Debugging
Article:
The StackTrace class gives access to the call stack for a specific thread. Each StackTrace object contains one or more StackFrame object. A StackFrame contains the information for a single method call. The code below uses the above objects along with the Reflection class to provide detailed information about the call stack: using System; using System.Text; using System.Diagnostics; using System.Reflection; namespace vbUsers.Debugging { class StackTraceDemo { [STAThread] static void Main(string[] args) { // Console.WriteLine("Stack Trace: \n\r" + StackTraceDump(false)); Console.Read(); // } ///
/// Returns a formated string containing the call stack. ///
///
Returns a formated string containing the call stack.
public static string StackTraceDump() { //Create a buffer to store output StringBuilder sbResult = new StringBuilder(500); //Get the stack trace including file info StackTrace trcStack = new StackTrace(true); int iFrameCount = trcStack.FrameCount; int iStartFrame; //Don't include this method in call stack iStartFrame = 1; //Loop over stack frames for(int n = iStartFrame; n < iFrameCount; ++n) { //Get current frame information StackFrame fraCurrent = trcStack.GetFrame(n); if ( fraCurrent != null ) { int iLineNo = fraCurrent.GetFileLineNumber(); string fileName = PathFileToFile(fraCurrent.GetFileName()); if ( fileName.Length == 0 ) { fileName = "[No File]"; } //Use Reflection to get method information MethodBase mtdCurrent = fraCurrent.GetMethod(); if ( mtdCurrent != null ) { string methodName = mtdCurrent.Name; ParameterInfo [] apaInfo = mtdCurrent.GetParameters(); sbResult.AppendFormat("File {0}, Line {1}, Method {2}", fileName,iLineNo,methodName); //Get parameter information if(apaInfo != null && apaInfo.Length == 0) { //No parameters for method sbResult.Append("()\n\r"); } else { //Iterate over parameters sbResult.Append("("); int iCount = apaInfo.Length; for(int i = 0; i < iCount; ++i) { Type tpParam = apaInfo[i].ParameterType; sbResult.AppendFormat("{0} {1}",tpParam.ToString(),apaInfo[i].Name); if(i < iCount-1) { sbResult.Append(","); } } sbResult.Append(")\n\r"); } } else { //Null method sbResult.Append("[Null Method]\n\r"); } } else { //Null frame sbResult.Append("[Null Frame]\n\r"); } } return sbResult.ToString(); } //Returns a filename given a path and filename static string PathFileToFile(string PathFileName) { int iPos; iPos = PathFileName.LastIndexOf(@"\"); if(iPos > 0) return PathFileName.Substring(iPos+1); else return PathFileName; } //Returns a path given a path and filename public static string PathFileToPath(string pathFileName) { int iPos; iPos = pathFileName.LastIndexOf(@"\"); if(iPos > 0) return pathFileName.Substring(0,iPos + 1); else return pathFileName; } } }
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet