StackTrace Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Egy veremnyomkövetést jelent, amely egy vagy több veremkeret rendezett gyűjteményéből áll.
public ref class StackTrace sealed
public ref class StackTrace
public sealed class StackTrace
public class StackTrace
[System.Serializable]
public class StackTrace
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StackTrace
type StackTrace = class
[<System.Serializable>]
type StackTrace = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type StackTrace = class
Public NotInheritable Class StackTrace
Public Class StackTrace
- Öröklődés
-
StackTrace
- Attribútumok
Példák
Az alábbi konzolalkalmazás bemutatja, hogyan hozhat létre egy egyszerű StackTrace és iterátumot a kereteken keresztül a hibakeresési és diagnosztikai információk beszerzéséhez.
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
*/
Imports System.Diagnostics
Class StackTraceSample
<STAThread()> _
Public Shared Sub Main()
Dim sample As New StackTraceSample()
Try
sample.MyPublicMethod()
Catch
' Create a StackTrace that captures
' filename, line number, and column
' information for the current thread.
Dim st As New StackTrace(True)
Dim i As Integer
For i = 0 To st.FrameCount - 1
' Note that high up the call stack, there is only
' one stack frame.
Dim sf As StackFrame = 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())
Next i
End Try
End Sub
Public Sub MyPublicMethod()
MyProtectedMethod()
End Sub
Protected Sub MyProtectedMethod()
Dim mic As New MyInternalClass()
mic.ThrowsException()
End Sub
Class MyInternalClass
Public Sub ThrowsException()
Try
Throw New Exception("A problem was encountered.")
Catch e As Exception
' Create a StackTrace that captures filename,
' line number and column information.
Dim st As New StackTrace(True)
Dim stackIndent As String = ""
Dim i As Integer
For i = 0 To st.FrameCount - 1
' Note that at this level, there are four
' stack frames, one for each method invocation.
Dim sf As StackFrame = 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 += " "
Next i
Throw e
End Try
End Sub
End Class
End Class
' This console application produces the following output when
' compiled with the Debug configuration.
'
' Method: Void ThrowsException()
' File: c:\pp\samples\stacktraceframe\myclass.vb
' Line Number: 55
'
' Method: Void MyProtectedMethod()
' File: c:\pp\samples\stacktraceframe\myclass.vb
' Line Number: 42
'
' Method: Void MyPublicMethod()
' File: c:\pp\samples\stacktraceframe\myclass.vb
' Line Number: 37
'
' Method: Void Main(System.String[])
' File: c:\pp\samples\stacktraceframe\myclass.vb
' Line Number: 13
'
' High up the call stack, Method: Void Main(System.String[])
' High up the call stack, Line Number: 18
'
'
' 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()
' High up the call stack, Line Number: 0
'
Megjegyzések
StackTrace A hibakeresési buildkonfigurációkkal kapcsolatos információk a leginformatívabbak. A hibakeresési buildek alapértelmezés szerint hibakeresési szimbólumokat tartalmaznak, míg a Release buildek nem. A hibakeresési szimbólumok a legtöbb fájl- és metódusnevet, sorszámot és oszlopinformációt tartalmazzák, amelyeket a szerkezetek és StackFrame objektumok használnakStackTrace.
StackTrace az optimalizálás során bekövetkező kódátalakítások miatt előfordulhat, hogy nem jelenti a várt számú metódushívást.
Konstruktorok
| Name | Description |
|---|---|
| StackTrace() |
Inicializálja az StackTrace osztály új példányát a hívó keretéből. |
| StackTrace(Boolean) |
Inicializálja az osztály új példányát a StackTrace hívó keretéből, és opcionálisan rögzíti a forrásadatokat. |
| StackTrace(Exception, Boolean) |
Inicializálja az osztály új példányát a StackTrace megadott kivételobjektum használatával, és opcionálisan rögzíti a forrásadatokat. |
| StackTrace(Exception, Int32, Boolean) |
Inicializálja az osztály új példányát a StackTrace megadott kivételobjektum használatával, kihagyja a megadott számú keretet, és opcionálisan rögzíti a forrásadatokat. |
| StackTrace(Exception, Int32) |
Inicializálja az osztály új példányát a StackTrace megadott kivételobjektum használatával, és kihagyja a megadott számú keretet. |
| StackTrace(Exception) |
Inicializálja az osztály új példányát a StackTrace megadott kivételobjektum használatával. |
| StackTrace(IEnumerable<StackFrame>) |
Objektumkészletből StackFrame készít veremkövetőt. |
| StackTrace(Int32, Boolean) |
Inicializálja az osztály új példányát a StackTrace hívó keretéből, kihagyja a megadott számú keretet, és opcionálisan rögzíti a forrásadatokat. |
| StackTrace(Int32) |
Inicializálja az StackTrace osztály új példányát a hívó keretéből, kihagyva a megadott számú keretet. |
| StackTrace(StackFrame) |
Inicializálja az StackTrace osztály egy új példányát, amely egyetlen keretet tartalmaz. |
| StackTrace(Thread, Boolean) |
Elavult.
Inicializálja az StackTrace osztály egy új példányát egy adott szálhoz, opcionálisan rögzíti a forrásadatokat. Ne használja ezt a konstruktor túlterhelést. |
Mezők
| Name | Description |
|---|---|
| METHODS_TO_SKIP |
A verem nyomkövetéséből kihagyandó metódusok számának alapértelmezett értékét határozza meg. Ez a mező állandó. |
Tulajdonságok
| Name | Description |
|---|---|
| FrameCount |
Lekéri a verem nyomkövetésében lévő keretek számát. |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal. (Öröklődés forrása Object) |
| GetFrame(Int32) |
Lekéri a megadott veremkeretet. |
| GetFrames() |
Az aktuális veremkövetés összes veremkeretének másolatát adja vissza. |
| GetHashCode() |
Ez az alapértelmezett kivonatoló függvény. (Öröklődés forrása Object) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| ToString() |
Olvashatóan ábrázolja a verem nyomkövetését. |