StackTrace Klass
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Representerar ett stackspår, som är en ordnad samling av en eller flera stackramar.
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
- Arv
-
StackTrace
- Attribut
Exempel
Följande konsolprogram visar hur du skapar en enkel StackTrace och iterera genom dess ramar för att hämta felsöknings- och diagnostikinformation.
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
'
Kommentarer
StackTrace information kommer att vara mest informativ med felsökningsversionskonfigurationer. Som standard innehåller felsökningsversioner felsökningssymboler, medan Versionsversioner inte gör det. Felsökningssymbolerna innehåller de flesta av de filer, metodnamn, radnummer och kolumninformation som används vid konstruktion StackFrame och StackTrace objekt.
StackTrace kanske inte rapporterar så många metodanrop som förväntat på grund av kodtransformeringar som sker under optimeringen.
Konstruktorer
| Name | Description |
|---|---|
| StackTrace() |
Initierar en ny instans av StackTrace klassen från anroparens ram. |
| StackTrace(Boolean) |
Initierar en ny instans av StackTrace klassen från anroparens ram, och kan också samla in källinformation. |
| StackTrace(Exception, Boolean) |
Initierar en ny instans av klassen med hjälp av StackTrace det angivna undantagsobjektet och kan också samla in källinformation. |
| StackTrace(Exception, Int32, Boolean) |
Initierar en ny instans av klassen med hjälp av StackTrace det angivna undantagsobjektet, hoppar över det angivna antalet bildrutor och kan också samla in källinformation. |
| StackTrace(Exception, Int32) |
Initierar en ny instans av klassen med hjälp av StackTrace det angivna undantagsobjektet och hoppar över det angivna antalet bildrutor. |
| StackTrace(Exception) |
Initierar en ny instans av klassen med hjälp av StackTrace det angivna undantagsobjektet. |
| StackTrace(IEnumerable<StackFrame>) |
Konstruerar en stackspårning från en uppsättning StackFrame objekt. |
| StackTrace(Int32, Boolean) |
Initierar en ny instans av StackTrace klassen från anroparens ram, hoppar över det angivna antalet bildrutor och kan också samla in källinformation. |
| StackTrace(Int32) |
Initierar en ny instans av StackTrace klassen från anroparens ram och hoppar över det angivna antalet bildrutor. |
| StackTrace(StackFrame) |
Initierar en ny instans av StackTrace klassen som innehåller en enda ram. |
| StackTrace(Thread, Boolean) |
Föråldrad.
Initierar en ny instans av StackTrace klassen för en specifik tråd, om du vill samla in källinformation. Använd inte den här konstruktorns överlagring. |
Fält
| Name | Description |
|---|---|
| METHODS_TO_SKIP |
Definierar standardvärdet för antalet metoder som ska utelämnas från stackspårningen. Det här fältet är konstant. |
Egenskaper
| Name | Description |
|---|---|
| FrameCount |
Hämtar antalet bildrutor i stackspårningen. |
Metoder
| Name | Description |
|---|---|
| Equals(Object) |
Avgör om det angivna objektet är lika med det aktuella objektet. (Ärvd från Object) |
| GetFrame(Int32) |
Hämtar den angivna stackramen. |
| GetFrames() |
Returnerar en kopia av alla stackramar i den aktuella stackspårningen. |
| GetHashCode() |
Fungerar som standard-hash-funktion. (Ärvd från Object) |
| GetType() |
Hämtar den aktuella instansen Type . (Ärvd från Object) |
| MemberwiseClone() |
Skapar en ytlig kopia av den aktuella Object. (Ärvd från Object) |
| ToString() |
Skapar en läsbar representation av stackspårningen. |