StackTrace 构造函数

定义

初始化 StackTrace 类的新实例。

重载

名称 说明
StackTrace()

从调用方框架初始化类的新实例 StackTrace

StackTrace(Boolean)

从调用方帧初始化类的新实例 StackTrace ,可以选择捕获源信息。

StackTrace(IEnumerable<StackFrame>)

从一组 StackFrame 对象构造堆栈跟踪。

StackTrace(StackFrame)

初始化包含单个帧的 StackTrace 类的新实例。

StackTrace(Exception)

使用提供的异常对象初始化类的新实例 StackTrace

StackTrace(Int32)

从调用方帧初始化类的新实例 StackTrace ,跳过指定的帧数。

StackTrace(Exception, Int32)

使用提供的异常对象初始化类的新实例 StackTrace ,并跳过指定的帧数。

StackTrace(Int32, Boolean)

从调用方帧初始化类的新实例 StackTrace ,跳过指定的帧数并选择性地捕获源信息。

StackTrace(Thread, Boolean)
已过时.

为特定线程初始化类的新实例 StackTrace ,可以选择捕获源信息。

请勿使用此构造函数重载。

StackTrace(Exception, Int32, Boolean)

使用提供的异常对象初始化类的新实例 StackTrace ,跳过指定的帧数并选择性地捕获源信息。

StackTrace(Exception, Boolean)

使用提供的异常对象和选择性地捕获源信息初始化类的新实例 StackTrace

StackTrace()

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

从调用方框架初始化类的新实例 StackTrace

public:
 StackTrace();
public StackTrace();
Public Sub New ()

示例

下面的代码示例显示堆栈跟踪中的第一个和最后一个函数调用。

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 调用方当前线程创建,并且不包含文件名、行号或列信息。

如果想要包含有关调用堆栈的摘要方法信息的完整跟踪,请使用此无参数构造函数。

适用于

StackTrace(Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

从调用方帧初始化类的新实例 StackTrace ,可以选择捕获源信息。

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.

示例

下面的代码示例演示了各种 StackTrace 构造函数方法。

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 调用方当前线程创建。

适用于

StackTrace(IEnumerable<StackFrame>)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

从一组 StackFrame 对象构造堆栈跟踪。

public:
 StackTrace(System::Collections::Generic::IEnumerable<System::Diagnostics::StackFrame ^> ^ frames);
public StackTrace(System.Collections.Generic.IEnumerable<System.Diagnostics.StackFrame> frames);
new System.Diagnostics.StackTrace : seq<System.Diagnostics.StackFrame> -> System.Diagnostics.StackTrace
Public Sub New (frames As IEnumerable(Of StackFrame))

参数

frames
IEnumerable<StackFrame>

堆栈跟踪中应存在的堆栈帧集。

适用于

StackTrace(StackFrame)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

初始化包含单个帧的 StackTrace 类的新实例。

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 框架。

示例

下面的代码示例将堆栈跟踪信息写入事件日志条目。

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)

注解

如果不希望完全堆栈跟踪的开销,请使用此构造函数。

另请参阅

适用于

StackTrace(Exception)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

使用提供的异常对象初始化类的新实例 StackTrace

public:
 StackTrace(Exception ^ e);
public StackTrace(Exception e);
new System.Diagnostics.StackTrace : Exception -> System.Diagnostics.StackTrace
Public Sub New (e As Exception)

参数

e
Exception

从中构造堆栈跟踪的异常对象。

例外

参数 enull.

注解

使用 StackTrace 调用方当前线程创建,并且不包含文件名、行号或列信息。

生成的堆栈跟踪描述异常时堆栈。

另请参阅

适用于

StackTrace(Int32)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

从调用方帧初始化类的新实例 StackTrace ,跳过指定的帧数。

public:
 StackTrace(int skipFrames);
public StackTrace(int skipFrames);
new System.Diagnostics.StackTrace : int -> System.Diagnostics.StackTrace
Public Sub New (skipFrames As Integer)

参数

skipFrames
Int32

从中启动跟踪的堆栈的帧数。

例外

参数 skipFrames 为负数。

注解

使用 StackTrace 调用方当前线程创建,并且不包含文件名、行号或列信息。

如果要跳过的帧数大于或等于创建实例时调用堆栈上的帧总数, StackTrace 则不包含任何帧。

适用于

StackTrace(Exception, Int32)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

使用提供的异常对象初始化类的新实例 StackTrace ,并跳过指定的帧数。

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)

参数

e
Exception

从中构造堆栈跟踪的异常对象。

skipFrames
Int32

从中启动跟踪的堆栈的帧数。

例外

参数 enull.

参数 skipFrames 为负数。

注解

不包含 StackTrace 文件名、行号或列信息。

生成的堆栈跟踪描述异常时堆栈。

如果要跳过的帧数大于或等于创建实例时调用堆栈上的帧总数, StackTrace 则不包含任何帧。

另请参阅

适用于

StackTrace(Int32, Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

从调用方帧初始化类的新实例 StackTrace ,跳过指定的帧数并选择性地捕获源信息。

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

从中启动跟踪的堆栈的帧数。

fNeedFileInfo
Boolean

true 捕获文件名、行号和列号;否则,为 false.

例外

参数 skipFrames 为负数。

示例

下面的代码示例演示了各种 StackTrace 构造函数方法。

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 则不包含任何帧。

适用于

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

为特定线程初始化类的新实例 StackTrace ,可以选择捕获源信息。

请勿使用此构造函数重载。

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);
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
Public Sub New (targetThread As Thread, needFileInfo As Boolean)

参数

targetThread
Thread

请求堆栈跟踪的线程。

needFileInfo
Boolean

true 捕获文件名、行号和列号;否则,为 false.

属性

例外

线程 targetThread 未挂起。

注解

Important

请勿使用此构造函数。 它已过时,不建议使用替代方法。 暂停线程时,无法知道它正在执行的代码,并且死锁很容易发生。 例如,如果在安全权限评估期间暂停线程,则可能会阻止线程中的其他 AppDomain 线程。 如果在执行类构造函数时挂起线程,则尝试使用该类的其他线程 AppDomain 将被阻止。

另请参阅

适用于

StackTrace(Exception, Int32, Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

使用提供的异常对象初始化类的新实例 StackTrace ,跳过指定的帧数并选择性地捕获源信息。

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)

参数

e
Exception

从中构造堆栈跟踪的异常对象。

skipFrames
Int32

从中启动跟踪的堆栈的帧数。

fNeedFileInfo
Boolean

true 捕获文件名、行号和列号;否则,为 false.

例外

参数 enull.

参数 skipFrames 为负数。

注解

生成的堆栈跟踪描述异常时堆栈。

如果要跳过的帧数大于或等于创建实例时调用堆栈上的帧总数, StackTrace 则不包含任何帧。

另请参阅

适用于

StackTrace(Exception, Boolean)

Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs
Source:
StackTrace.cs

使用提供的异常对象和选择性地捕获源信息初始化类的新实例 StackTrace

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

从中构造堆栈跟踪的异常对象。

needFileInfofNeedFileInfo
Boolean

true 捕获文件名、行号和列号;否则,为 false.

例外

参数 enull.

注解

生成的堆栈跟踪描述异常时堆栈。

另请参阅

适用于