StackTrace クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
スタック トレースを表します。スタック トレースは、順番に並べられた 1 つまたは複数のスタック フレームのコレクションです。
public ref class StackTrace sealed
public ref class StackTrace
public sealed class StackTrace
[System.Serializable]
public class StackTrace
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class StackTrace
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
- 継承
-
StackTrace
- 属性
例
次のコンソール アプリケーションは、単純な StackTrace を作成し、そのフレームを反復処理してデバッグと診断情報を取得する方法を示しています。
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
'
注釈
StackTrace の情報はデバッグビルド構成で最も情報量が多くなります。 既定では、デバッグ ビルドにはデバッグ シンボルが含まれますが、リリース ビルドには含まれません。 デバッグ シンボルには、 StackFrame オブジェクトと StackTrace オブジェクトの構築に使用されるファイル、メソッド名、行番号、列の情報の大部分が含まれています。
StackTrace は、最適化中に発生するコード変換により、予想される数のメソッド呼び出しを報告しない可能性があります。
コンストラクター
| 名前 | 説明 |
|---|---|
| StackTrace() |
呼び出し元のフレームから StackTrace クラスの新しいインスタンスを初期化します。 |
| StackTrace(Boolean) |
呼び出し元のフレームから StackTrace クラスの新しいインスタンスを初期化し、必要に応じてソース情報をキャプチャします。 |
| StackTrace(Exception, Boolean) |
指定された例外オブジェクトを使用し、必要に応じてソース情報をキャプチャして、 StackTrace クラスの新しいインスタンスを初期化します。 |
| StackTrace(Exception, Int32, Boolean) |
指定した例外オブジェクトを使用して StackTrace クラスの新しいインスタンスを初期化し、指定したフレーム数をスキップし、必要に応じてソース情報をキャプチャします。 |
| StackTrace(Exception, Int32) |
指定した例外オブジェクトを使用し、指定したフレーム数をスキップして、 StackTrace クラスの新しいインスタンスを初期化します。 |
| StackTrace(Exception) |
指定された例外オブジェクトを使用して、 StackTrace クラスの新しいインスタンスを初期化します。 |
| StackTrace(Int32, Boolean) |
呼び出し元のフレームから StackTrace クラスの新しいインスタンスを初期化し、指定した数のフレームをスキップし、必要に応じてソース情報をキャプチャします。 |
| StackTrace(Int32) |
指定したフレーム数をスキップして、呼び出し元のフレームから StackTrace クラスの新しいインスタンスを初期化します。 |
| StackTrace(StackFrame) |
1 つのフレームを含む StackTrace クラスの新しいインスタンスを初期化します。 |
| StackTrace(Thread, Boolean) |
古い.
必要に応じてソース情報をキャプチャして、特定のスレッドの StackTrace クラスの新しいインスタンスを初期化します。 このコンストラクターのオーバーロードは使用しないでください。 |
フィールド
| 名前 | 説明 |
|---|---|
| METHODS_TO_SKIP |
スタック トレースから省略するメソッドの数の既定値を定義します。 このフィールドは定数です。 |
プロパティ
| 名前 | 説明 |
|---|---|
| FrameCount |
スタック トレース内のフレーム数を取得します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| GetFrame(Int32) |
指定したスタック フレームを取得します。 |
| GetFrames() |
現在のスタック トレース内のすべてのスタック フレームのコピーを返します。 |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| ToString() |
スタック トレースの読み取り可能な表現を構築します。 |