如何在异常中打印完整堆栈跟踪?

时间:2010-11-24 23:47:24

标签: c# exception-handling stack-trace

例如,在一个地方......

//---------------a
try
{
    // some network call
}
catch(WebException we)
{
    throw new MyCustomException("some message ....", we);
}

......在另一个地方......

//--------------b
try
{
    // invoke code above
}
catch(MyCustomException we)
{
    Debug.Writeline(we.stacktrace);   // <----------------
}

我打印的堆栈跟踪,它只从a到b开始, 它不包含WebException中的内部堆栈跟踪。

如何打印所有堆栈跟踪???

4 个答案:

答案 0 :(得分:151)

我通常在异常上使用.ToString()方法在文本中显示完整的异常信息(包括内部堆栈跟踪):

catch (MyCustomException ex)
{
    Debug.Writeline(ex.ToString());
}

示例输出:

ConsoleApplication1.MyCustomException: some message .... ---> System.Exception: Oh noes!
   at ConsoleApplication1.SomeObject.OtherMethod() in C:\ConsoleApplication1\SomeObject.cs:line 24
   at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 14
   --- End of inner exception stack trace ---
   at ConsoleApplication1.SomeObject..ctor() in C:\ConsoleApplication1\SomeObject.cs:line 18
   at ConsoleApplication1.Program.DoSomething() in C:\ConsoleApplication1\Program.cs:line 23
   at ConsoleApplication1.Program.Main(String[] args) in C:\ConsoleApplication1\Program.cs:line 13

答案 1 :(得分:51)

使用这样的函数:

    public static string FlattenException(Exception exception)
    {
        var stringBuilder = new StringBuilder();

        while (exception != null)
        {
            stringBuilder.AppendLine(exception.Message);
            stringBuilder.AppendLine(exception.StackTrace);

            exception = exception.InnerException;
        }

        return stringBuilder.ToString();
    }

然后你可以这样称呼它:

try
{
    // invoke code above
}
catch(MyCustomException we)
{
    Debug.Writeline(FlattenException(we));
}

答案 2 :(得分:11)

如果将异常传递给以下函数,它将为您提供所有方法和详细信息,这些都是异常的原因。

public string GetAllFootprints(Exception x)
{
        var st = new StackTrace(x, true);
        var frames = st.GetFrames();
        var traceString = new StringBuilder();

        foreach (var frame in frames)
        {
            if (frame.GetFileLineNumber() < 1)
                continue;

            traceString.Append("File: " + frame.GetFileName());
            traceString.Append(", Method:" + frame.GetMethod().Name);
            traceString.Append(", LineNumber: " + frame.GetFileLineNumber());
            traceString.Append("  -->  ");
        }

        return traceString.ToString();
}

<强>结果:

文件:c:\ MyProject \ Program.cs,方法:MyFunction,LineNumber:29 - &gt;

文件:c:\ MyProject \ Program.cs,方法:Main,LineNumber:16 - &gt;

答案 3 :(得分:0)

推荐使用LINQPad相关的nuget包,然后就可以使用exceptionInstance.Dump()

enter image description here

对于 .NET 核心:

  • 安装LINQPad.Runtime

对于 .NET framework 4 等

  • 安装LINQPad

示例代码:

using System;
using LINQPad;

namespace csharp_Dump_test
{
    public class Program
    {
        public static void Main()
        {
            try
            {
                dosome();
            }
            catch (Exception ex)
            {
                ex.Dump();
            }
        }

        private static void dosome()
        {
            throw new Exception("Unable.");
        }
    }
}

运行结果: enter image description here

LinqPad nuget 包是打印异常堆栈信息的最棒的工具。 愿对你有帮助。