StackTrace 构造函数
定义
初始化 StackTrace 类的新实例。Initializes a new instance of the StackTrace class.
重载
| StackTrace() |
用调用方的帧初始化 StackTrace 类的新实例。Initializes a new instance of the StackTrace class from the caller's frame. |
| StackTrace(Boolean) |
用调用方的帧初始化 StackTrace 类的新实例,可以选择捕获源信息。Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information. |
| StackTrace(StackFrame) |
初始化包含单个帧的 StackTrace 类的新实例。Initializes a new instance of the StackTrace class that contains a single frame. |
| StackTrace(Exception) |
使用提供的异常对象初始化 StackTrace 类的新实例。Initializes a new instance of the StackTrace class using the provided exception object. |
| StackTrace(Int32) |
从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数。Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames. |
| StackTrace(Exception, Int32) |
使用提供的异常对象初始化 StackTrace 类的新实例,并跳过指定的帧数。Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames. |
| StackTrace(Int32, Boolean) |
从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames and optionally capturing source information. |
| StackTrace(Thread, Boolean) |
已过时。
已过时。
初始化特定线程的 StackTrace 类的新实例,可以选择捕获源信息。Initializes a new instance of the StackTrace class for a specific thread, optionally capturing source information. 不要使用此构造函数重载。Do not use this constructor overload. |
| StackTrace(Exception, Int32, Boolean) |
使用提供的异常对象初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。Initializes a new instance of the StackTrace class using the provided exception object, skipping the specified number of frames and optionally capturing source information. |
| StackTrace(Exception, Boolean) |
使用所提供的异常对象初始化 StackTrace 类的新实例,可以选择捕获源信息。Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information. |
StackTrace()
用调用方的帧初始化 StackTrace 类的新实例。Initializes a new instance of the StackTrace class from the caller's frame.
public:
StackTrace();
public StackTrace ();
Public Sub New ()
示例
下面的代码示例显示堆栈跟踪中的第一个和最后一个函数调用。The following code example displays the first and last function calls in a stack trace.
void Level5Method()
{
try
{
ClassLevel6^ nestedClass = gcnew ClassLevel6;
nestedClass->Level6Method();
}
catch ( Exception^ e )
{
Console::WriteLine( " Level5Method exception handler" );
StackTrace^ st = gcnew StackTrace;
// Display the most recent function call.
StackFrame^ sf = st->GetFrame( 0 );
Console::WriteLine();
Console::WriteLine( " Exception in method: " );
Console::WriteLine( " {0}", sf->GetMethod() );
if ( st->FrameCount > 1 )
{
// Display the highest-level function call
// in the trace.
sf = st->GetFrame( st->FrameCount - 1 );
Console::WriteLine( " Original function call at top of call stack):" );
Console::WriteLine( " {0}", sf->GetMethod() );
}
Console::WriteLine();
Console::WriteLine( " ... throwing exception to next level ..." );
Console::WriteLine( "-------------------------------------------------\n" );
throw e;
}
}
public void Level5Method()
{
try
{
ClassLevel6 nestedClass = new ClassLevel6();
nestedClass.Level6Method();
}
catch (Exception e)
{
Console.WriteLine(" Level5Method exception handler");
StackTrace st = new StackTrace();
// Display the most recent function call.
StackFrame sf = st.GetFrame(0);
Console.WriteLine();
Console.WriteLine(" Exception in method: ");
Console.WriteLine(" {0}", sf.GetMethod());
if (st.FrameCount >1)
{
// Display the highest-level function call
// in the trace.
sf = st.GetFrame(st.FrameCount-1);
Console.WriteLine(" Original function call at top of call stack):");
Console.WriteLine(" {0}", sf.GetMethod());
}
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
Public Sub Level5Method()
Try
Dim nestedClass As New ClassLevel6()
nestedClass.Level6Method()
Catch e As Exception
Console.WriteLine(" Level5Method exception handler")
Dim st As New StackTrace()
' Display the most recent function call.
Dim sf As StackFrame = st.GetFrame(0)
Console.WriteLine()
Console.WriteLine(" Exception in method: ")
Console.WriteLine(" {0}", sf.GetMethod())
If st.FrameCount > 1 Then
' Display the highest-level function call in the trace.
sf = st.GetFrame((st.FrameCount - 1))
Console.WriteLine(" Original function call at top of call stack):")
Console.WriteLine(" {0}", sf.GetMethod())
End If
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
注解
StackTrace是用调用方的当前线程创建的,并且不包含文件名、行号或列信息。The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.
如果需要包含有关调用堆栈的摘要方法信息的完整跟踪,请使用此无参数的构造函数。Use this parameterless constructor when you want a complete trace with only summary method information about the call stack.
适用于
StackTrace(Boolean)
用调用方的帧初始化 StackTrace 类的新实例,可以选择捕获源信息。Initializes a new instance of the StackTrace class from the caller's frame, optionally capturing source information.
public:
StackTrace(bool fNeedFileInfo);
public StackTrace (bool fNeedFileInfo);
new System.Diagnostics.StackTrace : bool -> System.Diagnostics.StackTrace
Public Sub New (fNeedFileInfo As Boolean)
参数
- fNeedFileInfo
- Boolean
如果为 true,则捕获文件名、行号和列号;否则为 false。true to capture the file name, line number, and column number; otherwise, false.
示例
下面的代码示例演示了各种 StackTrace 构造函数方法。The following code example demonstrates various StackTrace constructor methods.
void Level2Method()
{
try
{
ClassLevel3^ nestedClass = gcnew ClassLevel3;
nestedClass->Level3Method();
}
catch ( Exception^ e )
{
Console::WriteLine( " Level2Method exception handler" );
// Display the full call stack at this level.
StackTrace^ st1 = gcnew StackTrace( true );
Console::WriteLine( " Stack trace for this level: {0}", st1->ToString() );
// Build a stack trace from one frame, skipping the
// current frame and using the next frame.
StackTrace^ st2 = gcnew StackTrace( gcnew StackFrame( 1,true ) );
Console::WriteLine( " Stack trace built with next level frame: {0}", st2->ToString() );
// Build a stack trace skipping the current frame, and
// including all the other frames.
StackTrace^ st3 = gcnew StackTrace( 1,true );
Console::WriteLine( " Stack trace built from the next level up: {0}", st3->ToString() );
Console::WriteLine();
Console::WriteLine( " ... throwing exception to next level ..." );
Console::WriteLine( "-------------------------------------------------\n" );
throw e;
}
}
public void Level2Method()
{
try
{
ClassLevel3 nestedClass = new ClassLevel3();
nestedClass.Level3Method();
}
catch (Exception e)
{
Console.WriteLine(" Level2Method exception handler");
// Display the full call stack at this level.
StackTrace st1 = new StackTrace(true);
Console.WriteLine(" Stack trace for this level: {0}",
st1.ToString());
// Build a stack trace from one frame, skipping the current
// frame and using the next frame.
StackTrace st2 = new StackTrace(new StackFrame(1, true));
Console.WriteLine(" Stack trace built with next level frame: {0}",
st2.ToString());
// Build a stack trace skipping the current frame, and
// including all the other frames.
StackTrace st3 = new StackTrace(1, true);
Console.WriteLine(" Stack trace built from the next level up: {0}",
st3.ToString());
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
Public Sub Level2Method()
Try
Dim nestedClass As New ClassLevel3
nestedClass.Level3Method()
Catch e As Exception
Console.WriteLine(" Level2Method exception handler")
' Display the full call stack at this level.
Dim st1 As New StackTrace(True)
Console.WriteLine(" Stack trace for this level: {0}", _
st1.ToString())
' Build a stack trace from one frame, skipping the current
' frame and using the next frame.
Dim st2 As New StackTrace(New StackFrame(1, True))
Console.WriteLine(" Stack trace built with next level frame: {0}", _
st2.ToString())
' Build a stack trace skipping the current frame, and
' including all the other frames.
Dim st3 As New StackTrace(1, True)
Console.WriteLine(" Stack trace built from the next level up: {0}", _
st3.ToString())
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
注解
StackTrace是用调用方的当前线程创建的。The StackTrace is created with the caller's current thread.
适用于
StackTrace(StackFrame)
初始化包含单个帧的 StackTrace 类的新实例。Initializes a new instance of the StackTrace class that contains a single frame.
public:
StackTrace(System::Diagnostics::StackFrame ^ frame);
public StackTrace (System.Diagnostics.StackFrame frame);
new System.Diagnostics.StackTrace : System.Diagnostics.StackFrame -> System.Diagnostics.StackTrace
Public Sub New (frame As StackFrame)
参数
- frame
- StackFrame
StackTrace 对象应包含的帧。The frame that the StackTrace object should contain.
示例
下面的代码示例将堆栈跟踪信息写入事件日志项。The following code example writes stack trace information to an event log entry.
StackFrame^ fr = gcnew StackFrame( 1,true );
StackTrace^ st = gcnew StackTrace( fr );
EventLog::WriteEntry( fr->GetMethod()->Name, st->ToString(), EventLogEntryType::Warning );
StackFrame fr = new StackFrame(1,true);
StackTrace st = new StackTrace(fr);
EventLog.WriteEntry(fr.GetMethod().Name,
st.ToString(),
EventLogEntryType.Warning);
Dim frame As New StackFrame(1, True)
Dim strace As New StackTrace(frame)
EventLog.WriteEntry(frame.GetMethod().Name, _
strace.ToString(), _
EventLogEntryType.Warning)
注解
如果不想要执行完整堆栈跟踪的开销,请使用此构造函数。Use this constructor when you do not want the overhead of a full stack trace.
另请参阅
适用于
StackTrace(Exception)
使用提供的异常对象初始化 StackTrace 类的新实例。Initializes a new instance of the StackTrace class using the provided exception object.
public:
StackTrace(Exception ^ e);
public StackTrace (Exception e);
new System.Diagnostics.StackTrace : Exception -> System.Diagnostics.StackTrace
Public Sub New (e As Exception)
参数
从其构造堆栈跟踪的异常对象。The exception object from which to construct the stack trace.
例外
参数 e 为 null。The parameter e is null.
注解
StackTrace是用调用方的当前线程创建的,并且不包含文件名、行号或列信息。The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.
生成的堆栈跟踪说明了在发生异常时的堆栈。The resulting stack trace describes the stack at the time of the exception.
另请参阅
适用于
StackTrace(Int32)
从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数。Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames.
public:
StackTrace(int skipFrames);
public StackTrace (int skipFrames);
new System.Diagnostics.StackTrace : int -> System.Diagnostics.StackTrace
Public Sub New (skipFrames As Integer)
参数
- skipFrames
- Int32
堆栈中的帧数,将从其上开始跟踪。The number of frames up the stack from which to start the trace.
例外
skipFrames 参数为负。The skipFrames parameter is negative.
注解
StackTrace是用调用方的当前线程创建的,并且不包含文件名、行号或列信息。The StackTrace is created with the caller's current thread, and does not contain file name, line number, or column information.
如果要跳过的帧数大于或等于创建实例时调用堆栈上的总帧数,则 StackTrace 将不包含帧。If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.
适用于
StackTrace(Exception, Int32)
使用提供的异常对象初始化 StackTrace 类的新实例,并跳过指定的帧数。Initializes a new instance of the StackTrace class using the provided exception object and skipping the specified number of frames.
public:
StackTrace(Exception ^ e, int skipFrames);
public StackTrace (Exception e, int skipFrames);
new System.Diagnostics.StackTrace : Exception * int -> System.Diagnostics.StackTrace
Public Sub New (e As Exception, skipFrames As Integer)
参数
从其构造堆栈跟踪的异常对象。The exception object from which to construct the stack trace.
- skipFrames
- Int32
堆栈中的帧数,将从其上开始跟踪。The number of frames up the stack from which to start the trace.
例外
参数 e 为 null。The parameter e is null.
skipFrames 参数为负。The skipFrames parameter is negative.
注解
不 StackTrace 包含文件名、行号或列信息。The StackTrace does not contain file name, line number, or column information.
生成的堆栈跟踪说明了在发生异常时的堆栈。The resulting stack trace describes the stack at the time of the exception.
如果要跳过的帧数大于或等于创建实例时调用堆栈上的总帧数,则 StackTrace 将不包含帧。If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.
另请参阅
适用于
StackTrace(Int32, Boolean)
从调用方的帧初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。Initializes a new instance of the StackTrace class from the caller's frame, skipping the specified number of frames and optionally capturing source information.
public:
StackTrace(int skipFrames, bool fNeedFileInfo);
public StackTrace (int skipFrames, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : int * bool -> System.Diagnostics.StackTrace
Public Sub New (skipFrames As Integer, fNeedFileInfo As Boolean)
参数
- skipFrames
- Int32
堆栈中的帧数,将从其上开始跟踪。The number of frames up the stack from which to start the trace.
- fNeedFileInfo
- Boolean
如果为 true,则捕获文件名、行号和列号;否则为 false。true to capture the file name, line number, and column number; otherwise, false.
例外
skipFrames 参数为负。The skipFrames parameter is negative.
示例
下面的代码示例演示了各种 StackTrace 构造函数方法。The following code example demonstrates various StackTrace constructor methods.
void Level2Method()
{
try
{
ClassLevel3^ nestedClass = gcnew ClassLevel3;
nestedClass->Level3Method();
}
catch ( Exception^ e )
{
Console::WriteLine( " Level2Method exception handler" );
// Display the full call stack at this level.
StackTrace^ st1 = gcnew StackTrace( true );
Console::WriteLine( " Stack trace for this level: {0}", st1->ToString() );
// Build a stack trace from one frame, skipping the
// current frame and using the next frame.
StackTrace^ st2 = gcnew StackTrace( gcnew StackFrame( 1,true ) );
Console::WriteLine( " Stack trace built with next level frame: {0}", st2->ToString() );
// Build a stack trace skipping the current frame, and
// including all the other frames.
StackTrace^ st3 = gcnew StackTrace( 1,true );
Console::WriteLine( " Stack trace built from the next level up: {0}", st3->ToString() );
Console::WriteLine();
Console::WriteLine( " ... throwing exception to next level ..." );
Console::WriteLine( "-------------------------------------------------\n" );
throw e;
}
}
public void Level2Method()
{
try
{
ClassLevel3 nestedClass = new ClassLevel3();
nestedClass.Level3Method();
}
catch (Exception e)
{
Console.WriteLine(" Level2Method exception handler");
// Display the full call stack at this level.
StackTrace st1 = new StackTrace(true);
Console.WriteLine(" Stack trace for this level: {0}",
st1.ToString());
// Build a stack trace from one frame, skipping the current
// frame and using the next frame.
StackTrace st2 = new StackTrace(new StackFrame(1, true));
Console.WriteLine(" Stack trace built with next level frame: {0}",
st2.ToString());
// Build a stack trace skipping the current frame, and
// including all the other frames.
StackTrace st3 = new StackTrace(1, true);
Console.WriteLine(" Stack trace built from the next level up: {0}",
st3.ToString());
Console.WriteLine();
Console.WriteLine(" ... throwing exception to next level ...");
Console.WriteLine("-------------------------------------------------\n");
throw e;
}
}
Public Sub Level2Method()
Try
Dim nestedClass As New ClassLevel3
nestedClass.Level3Method()
Catch e As Exception
Console.WriteLine(" Level2Method exception handler")
' Display the full call stack at this level.
Dim st1 As New StackTrace(True)
Console.WriteLine(" Stack trace for this level: {0}", _
st1.ToString())
' Build a stack trace from one frame, skipping the current
' frame and using the next frame.
Dim st2 As New StackTrace(New StackFrame(1, True))
Console.WriteLine(" Stack trace built with next level frame: {0}", _
st2.ToString())
' Build a stack trace skipping the current frame, and
' including all the other frames.
Dim st3 As New StackTrace(1, True)
Console.WriteLine(" Stack trace built from the next level up: {0}", _
st3.ToString())
Console.WriteLine()
Console.WriteLine(" ... throwing exception to next level ...")
Console.WriteLine("-------------------------------------------------")
Console.WriteLine()
Throw e
End Try
End Sub
注解
如果要跳过的帧数大于或等于创建实例时调用堆栈上的总帧数,则 StackTrace 将不包含帧。If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.
适用于
StackTrace(Thread, Boolean)
注意
This constructor has been deprecated. Please use a constructor that does not require a Thread parameter. http://go.microsoft.com/fwlink/?linkid=14202
注意
此 API 现已过时。
初始化特定线程的 StackTrace 类的新实例,可以选择捕获源信息。Initializes a new instance of the StackTrace class for a specific thread, optionally capturing source information.
不要使用此构造函数重载。Do not use this constructor overload.
public:
StackTrace(System::Threading::Thread ^ targetThread, bool needFileInfo);
public StackTrace (System.Threading.Thread targetThread, bool needFileInfo);
[System.Obsolete("This constructor has been deprecated. Please use a constructor that does not require a Thread parameter. http://go.microsoft.com/fwlink/?linkid=14202")]
public StackTrace (System.Threading.Thread targetThread, bool needFileInfo);
[System.Obsolete]
public StackTrace (System.Threading.Thread targetThread, bool needFileInfo);
new System.Diagnostics.StackTrace : System.Threading.Thread * bool -> System.Diagnostics.StackTrace
[<System.Obsolete("This constructor has been deprecated. Please use a constructor that does not require a Thread parameter. http://go.microsoft.com/fwlink/?linkid=14202")>]
new System.Diagnostics.StackTrace : System.Threading.Thread * bool -> System.Diagnostics.StackTrace
[<System.Obsolete>]
new System.Diagnostics.StackTrace : System.Threading.Thread * bool -> System.Diagnostics.StackTrace
Public Sub New (targetThread As Thread, needFileInfo As Boolean)
参数
- targetThread
- Thread
请求其堆栈跟踪的线程。The thread whose stack trace is requested.
- needFileInfo
- Boolean
如果为 true,则捕获文件名、行号和列号;否则为 false。true to capture the file name, line number, and column number; otherwise, false.
- 属性
例外
targetThread 线程未暂停。The thread targetThread is not suspended.
注解
重要
不要使用此构造函数。Do not use this constructor. 它已过时,并且没有推荐的替代项。It is obsolete, and there is no recommended alternative. 在挂起线程时,你无法获知它正在执行的代码,并且很容易发生死锁。When you suspend a thread, you have no way of knowing what code it is executing, and deadlocks can occur very easily. 例如,如果在安全权限评估期间,您在线程保持锁定的情况下将其挂起,则 AppDomain 中的其他线程可能被阻止。For example, if you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. 如果在线程执行类构造函数时挂起该线程,则中的其他线程将 AppDomain 会阻止尝试使用该类。If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked.
另请参阅
适用于
StackTrace(Exception, Int32, Boolean)
使用提供的异常对象初始化 StackTrace 类的新实例,跳过指定的帧数并可以选择捕获源信息。Initializes a new instance of the StackTrace class using the provided exception object, skipping the specified number of frames and optionally capturing source information.
public:
StackTrace(Exception ^ e, int skipFrames, bool fNeedFileInfo);
public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : Exception * int * bool -> System.Diagnostics.StackTrace
Public Sub New (e As Exception, skipFrames As Integer, fNeedFileInfo As Boolean)
参数
从其构造堆栈跟踪的异常对象。The exception object from which to construct the stack trace.
- skipFrames
- Int32
堆栈中的帧数,将从其上开始跟踪。The number of frames up the stack from which to start the trace.
- fNeedFileInfo
- Boolean
如果为 true,则捕获文件名、行号和列号;否则为 false。true to capture the file name, line number, and column number; otherwise, false.
例外
参数 e 为 null。The parameter e is null.
skipFrames 参数为负。The skipFrames parameter is negative.
注解
生成的堆栈跟踪说明了在发生异常时的堆栈。The resulting stack trace describes the stack at the time of the exception.
如果要跳过的帧数大于或等于创建实例时调用堆栈上的总帧数,则 StackTrace 将不包含帧。If the number of frames to skip is greater than or equal to the total number of frames on the call stack at the time the instance is created, the StackTrace will contain no frames.
另请参阅
适用于
StackTrace(Exception, Boolean)
使用所提供的异常对象初始化 StackTrace 类的新实例,可以选择捕获源信息。Initializes a new instance of the StackTrace class, using the provided exception object and optionally capturing source information.
public:
StackTrace(Exception ^ exception, bool needFileInfo);
public:
StackTrace(Exception ^ e, bool fNeedFileInfo);
public StackTrace (Exception exception, bool needFileInfo);
public StackTrace (Exception e, bool fNeedFileInfo);
new System.Diagnostics.StackTrace : Exception * bool -> System.Diagnostics.StackTrace
new System.Diagnostics.StackTrace : Exception * bool -> System.Diagnostics.StackTrace
Public Sub New (exception As Exception, needFileInfo As Boolean)
Public Sub New (e As Exception, fNeedFileInfo As Boolean)
参数
- exceptione
- Exception
从其构造堆栈跟踪的异常对象。The exception object from which to construct the stack trace.
- needFileInfofNeedFileInfo
- Boolean
如果为 true,则捕获文件名、行号和列号;否则为 false。true to capture the file name, line number, and column number; otherwise, false.
例外
参数 e 为 null。The parameter e is null.
注解
生成的堆栈跟踪说明了在发生异常时的堆栈。The resulting stack trace describes the stack at the time of the exception.