StackTrace (Clase)
Ensamblado: mscorlib (en mscorlib.dll)
La información de StackTrace será más descriptiva con configuraciones de versiones de depuración. Las versiones de depuración incluyen símbolos de depuración de forma predeterminada; sin embargo, las versiones de lanzamiento no incluyen estos símbolos. Los símbolos de depuración contienen la mayor parte de la información de archivo, nombre de método, número de línea y columna que se utiliza para construir objetos StackFrame y StackTrace.
Es posible que StackTrace no informe de todas las llamadas a métodos tal y como se esperaba, debido a las transformaciones de código que pueden producirse durante la optimización.
En la siguiente aplicación de consola se muestra cómo crear un objeto StackTrace sencillo y cómo recorrer en iteración sus marcos para obtener información de depuración y diagnóstico.
using System; using System.Diagnostics; class StackTraceSample { [STAThread] static void Main(string[] args) { StackTraceSample sample = new StackTraceSample(); try { sample.MyPublicMethod(); } catch (Exception) { // Create a StackTrace that captures // filename, line number, and column // information for the current thread. 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()); } } } public void MyPublicMethod () { MyProtectedMethod(); } protected void MyProtectedMethod () { MyInternalClass mic = new MyInternalClass(); mic.ThrowsException(); } class MyInternalClass { public void ThrowsException() { try { throw new Exception("A problem was encountered."); } catch (Exception e) { // Create a StackTrace that captures filename, // line number and column information. StackTrace st = new StackTrace(true); string stackIndent = ""; for(int i =0; i< st.FrameCount; i++ ) { // Note that at this level, there are four // stack frames, one for each method invocation. StackFrame sf = st.GetFrame(i); Console.WriteLine(); Console.WriteLine(stackIndent + " Method: {0}", sf.GetMethod() ); Console.WriteLine( stackIndent + " File: {0}", sf.GetFileName()); Console.WriteLine( stackIndent + " Line Number: {0}", sf.GetFileLineNumber()); stackIndent += " "; } throw e; } } } } /* This console application produces the following output when compiled with the Debug configuration. Method: Void ThrowsException() File: c:\samples\stacktraceframe\myclass.cs Line Number: 59 Method: Void MyProtectedMethod() File: c:\samples\stacktraceframe\myclass.cs Line Number: 45 Method: Void MyPublicMethod() File: c:\samples\stacktraceframe\myclass.cs Line Number: 39 Method: Void Main(System.String[]) File: c:\samples\stacktraceframe\myclass.cs Line Number: 13 High up the call stack, Method: Void Main(System.String[]) High up the call stack, Line Number: 20 This console application produces the following output when compiled with the Release configuration. Method: Void ThrowsException() File: Line Number: 0 Method: Void Main(System.String[]) File: Line Number: 0 High up the call stack, Method: Void Main(System.String[]) High up the call stack, Line Number: 0 */
import System.*;
import System.Diagnostics.*;
class StackTraceSample
{
/** @attribute STAThread()
*/
public static void main(String[] args)
{
StackTraceSample sample = new StackTraceSample();
try {
sample.MyPublicMethod();
}
catch (System.Exception exp) {
// Create a StackTrace that captures
// filename, line number, and column
// information for the current thread.
StackTrace st = new StackTrace(true);
for (int i = 0; i < st.get_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}",
(Int32)sf.GetFileLineNumber());
}
}
} //main
public void MyPublicMethod() throws System.Exception
{
MyProtectedMethod();
} //MyPublicMethod
protected void MyProtectedMethod() throws System.Exception
{
MyInternalClass mic = new MyInternalClass();
mic.ThrowsException();
} //MyProtectedMethod
class MyInternalClass
{
public void ThrowsException() throws System.Exception
{
try {
throw new System.Exception("A problem was encountered.");
}
catch (System.Exception e) {
// Create a StackTrace that captures filename,
// line number and column information.
StackTrace st = new StackTrace(true);
String stackIndent = "";
for (int i = 0; i < st.get_FrameCount(); i++) {
// Note that at this level, there are four
// stack frames, one for each method invocation.
StackFrame sf = st.GetFrame(i);
Console.WriteLine();
Console.WriteLine(stackIndent + " Method: {0}",
sf.GetMethod());
Console.WriteLine(stackIndent + " File: {0}",
sf.GetFileName());
Console.WriteLine(stackIndent + " Line Number: {0}",
(Int32)sf.GetFileLineNumber());
stackIndent += " ";
}
throw e;
}
} //ThrowsException
} //MyInternalClass
} //StackTraceSample
/*
This console application produces the following output when
compiled with the Debug configuration.
Method: Void ThrowsException()
File: c:\samples\stacktraceframe\myclass.jsl
Line Number: 57
Method: Void MyProtectedMethod()
File: c:\samples\stacktraceframe\myclass.jsl
Line Number: 43
Method: Void MyPublicMethod()
File: c:\samples\stacktraceframe\myclass.jsl
Line Number: 37
Method: Void main(System.String[])
File: c:\samples\stacktraceframe\myclass.jsl
Line Number: 14
High up the call stack, Method: Void main(System.String[])
High up the call stack, Line Number: 21
This console application produces the following output when
compiled with the Release configuration.
Method: Void ThrowsException()
File:
Line Number: 0
Method: Void MyProtectedMethod()
File:
Line Number: 0
Method: Void MyPublicMethod()
File:
Line Number: 0
Method: Void main(System.String[])
File:
Line Number: 0
High up the call stack, Method: Void main(System.String[])
High up the call stack, Line Number: 0
*/
Windows 98, Windows 2000 SP4, Windows Millennium, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.