本文整理汇总了C#中System.Diagnostics.StackTrace类的典型用法代码示例。如果您正苦于以下问题:C# StackTrace类的具体用法?C# StackTrace怎么用?C# StackTrace使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StackTrace类属于System.Diagnostics命名空间,在下文中一共展示了StackTrace类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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
*/
开发者ID:.NET开发者,项目名称:System.Diagnostics,代码行数:119,代码来源:StackTrace
示例2: new StackTrace
//引入命名空间
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
public sealed class MainClass
{
static void Main()
{
StackTrace creationStackTrace = new StackTrace(1, true);
for( int i = 0;i < creationStackTrace.FrameCount; ++i ) {
StackFrame frame = creationStackTrace.GetFrame(i);
Console.WriteLine( " {0}", frame.ToString() );
}
}
}
开发者ID:C#程序员,项目名称:System.Diagnostics,代码行数:16,代码来源:StackTrace
注:本文中的System.Diagnostics.StackTrace类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论