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

用调用方的帧初始化 StackTrace 类的新实例。

C#
public StackTrace ();

示例

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

C#
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;
   }
}

注解

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

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

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Boolean)

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

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

C#
public StackTrace (bool fNeedFileInfo);

参数

fNeedFileInfo
Boolean

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

示例

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

C#
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;
   }
}

注解

StackTrace是使用调用方当前线程创建的。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(IEnumerable<StackFrame>)

Source:
StackTrace.cs
Source:
StackTrace.cs

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

C#
public StackTrace (System.Collections.Generic.IEnumerable<System.Diagnostics.StackFrame> frames);

参数

frames
IEnumerable<StackFrame>

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

适用于

.NET 9 和 .NET 8
产品 版本
.NET 8, 9

StackTrace(StackFrame)

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

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

C#
public StackTrace (System.Diagnostics.StackFrame frame);

参数

frame
StackFrame

StackTrace 对象应包含的帧。

示例

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

C#
StackFrame fr = new StackFrame(1,true);
StackTrace st = new StackTrace(fr);
EventLog.WriteEntry(fr.GetMethod().Name,
                    st.ToString(),
                    EventLogEntryType.Warning);

注解

如果不需要完整堆栈跟踪的开销,请使用此构造函数。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Exception)

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

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

C#
public StackTrace (Exception e);

参数

e
Exception

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

例外

参数 enull

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Int32)

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

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

C#
public StackTrace (int skipFrames);

参数

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

例外

skipFrames 参数为负。

注解

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

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

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Exception, Int32)

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

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

C#
public StackTrace (Exception e, int skipFrames);

参数

e
Exception

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

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

例外

参数 enull

skipFrames 参数为负。

注解

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

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Int32, Boolean)

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

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

C#
public StackTrace (int skipFrames, bool fNeedFileInfo);

参数

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

fNeedFileInfo
Boolean

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

例外

skipFrames 参数为负。

示例

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

C#
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;
   }
}

注解

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

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

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 类的新实例,可以选择捕获源信息。

不要使用此构造函数重载。

C#
public StackTrace (System.Threading.Thread targetThread, bool needFileInfo);
C#
[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);

参数

targetThread
Thread

请求其堆栈跟踪的线程。

needFileInfo
Boolean

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

属性

例外

targetThread 线程未暂停。

注解

重要

不要使用此构造函数。 它已过时,并且没有推荐的替代项。 在挂起线程时,你无法获知它正在执行的代码,并且很容易发生死锁。 例如,如果在安全权限评估期间,您在线程保持锁定的情况下将其挂起,则 AppDomain 中的其他线程可能被阻止。 如果在线程执行类构造函数时挂起该线程,则会阻止尝试使用该类的其他线程 AppDomain

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本 (已过时)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0 (4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1)

StackTrace(Exception, Int32, Boolean)

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

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

C#
public StackTrace (Exception e, int skipFrames, bool fNeedFileInfo);

参数

e
Exception

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

skipFrames
Int32

堆栈中的帧数,将从其上开始跟踪。

fNeedFileInfo
Boolean

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

例外

参数 enull

skipFrames 参数为负。

注解

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

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

StackTrace(Exception, Boolean)

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

使用所提供的异常对象初始化 StackTrace 类的新实例,可以选择捕获源信息。

C#
public StackTrace (Exception exception, bool needFileInfo);
C#
public StackTrace (Exception e, bool fNeedFileInfo);

参数

exceptione
Exception

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

needFileInfofNeedFileInfo
Boolean

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

例外

参数 enull

注解

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0