在try..catch中获取Exception对象,使其包含FULL stacktrace,当前已将其截断

| 有人可以帮忙吗? 我正在尝试在try..catch的范围内获取完整的stacktrace。目前,它被截断为仅包含错误所在的当前方法。 让我解释一下。目前,我的堆栈跟踪包含发生错误的方法“第三”,但不包括第一和第二,我相信这是设计使然。
private void First()
{
    this.Second();
}

private void Second()
{
    this.Third();
}

private void Third()
{
    try
    {
        throw new SystemException(\"ERROR HERE!\");
    }
    catch (Exception ex)
    {
        // I WILL LOG THE EXCEPTION object \"EX\" here ! but ex.StackTrace is truncated!
    }
}
我已经看到了许多技巧来获取STRING中的完整stacktrace,但是问题是我的日志记录框架期望对象的类型为“ Exception \”,但是具有异常的变量(ex)有效,但是StackTrace属性被截断了。 无论如何,我可以收到带有FULL Stacktrace的FULL异常,因此我仍然可以沿我的\“ EX \”发送,但是这一次它将具有未删节的stacktrace。 UnhandledErrror事件似乎可以正常工作,好像我到达这里时,异常具有堆栈跟踪并已完全填充... 我真的很感激任何反馈。     
已邀请:
class Program
    {
        static void Main(string[] args)
        {
            try
            {
                First();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            Console.ReadLine();
        }

        private static void First()
        {
            Second();

        }

        private static void Second()
        {
            Third();

        }

        private static void Third()
        {
            throw new SystemException(\"ERROR HERE!\");
        }
    }
在这种情况下的输出为:
   at stack.Program.Third() in C:\\Temp\\customers\\stack\\Program.cs:line 37
   at stack.Program.Second() in C:\\Temp\\customers\\stack\\Program.cs:line 31
   at stack.Program.First() in C:\\Temp\\customers\\stack\\Program.cs:line 25
   at stack.Program.Main(String[] args) in C:\\Temp\\customers\\stack\\Program.cs:li
ne 14
更新:我突然感到怀疑,并尝试了迄今为止提出的所有解决方案。它们都是可行的,尽管Environment.StackTrace是最简单的。输出结果如下所示:
==ex.StackTrace==
   at stack.Program.Third() in C:\\Temp\\customers\\stack\\Program.cs:line 35

==Environment.StackTrace per Guillaume/Jorge Córdoba==
   at stack.Program.Third() in C:\\Temp\\customers\\stack\\Program.cs:line 35   at S
ystem.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at stack.Program.Third() in C:\\Temp\\customers\\stack\\Program.cs:line 44
   at stack.Program.Second() in C:\\Temp\\customers\\stack\\Program.cs:line 27
   at stack.Program.First() in C:\\Temp\\customers\\stack\\Program.cs:line 21
   at stack.Program.Main(String[] args) in C:\\Temp\\customers\\stack\\Program.cs:li
ne 15
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySec
urity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

==ex.ToString() per danyolgiax==
System.SystemException: ERROR HERE!
   at stack.Program.Third() in C:\\Temp\\customers\\stack\\Program.cs:line 35

==GetFrame(i) per MBen==
Void Third(): (line 52)
Void Second(): (line 27)
Void First(): (line 21)
Void Main(System.String[]): (line 15)
Int32 _nExecuteAssembly(System.Reflection.Assembly, System.String[]): (line 0)
Int32 ExecuteAssembly(System.String, System.Security.Policy.Evidence, System.Str
ing[]): (line 0)
Void RunUsersAssembly(): (line 0)
Void ThreadStart_Context(System.Object): (line 0)
Void Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, Sy
stem.Object): (line 0)
Void ThreadStart(): (line 0)
    
string fullStackTrace = exception.StackTrace + Environment.StackTrace;
当前方法可能会被添加两次,如果这样,请从其中一个属性中删除一行。     
您需要这样的东西:
        private static void Third()
        {
        try
        {
            throw new SystemException(\"ERROR HERE!\");
        }
        catch (Exception ex)
        {
            // I WILL LOG THE EXCEPTION object \"EX\" here ! but ex.StackTrace is truncated!
            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());
            }
        }
    }
查看本文以获取更多信息: StackStace类 希望这可以帮助。     
您不了解堆栈跟踪的工作方式。异常跟踪中包含的是“路由”,简而言之就是异常已经传播到您要处理的位置。 因此,在您的示例中,它还没有运行,它仍然处于“ Third \”方法中。如果它上升到\“ Second \”,那么它将跨过Third,到Second,您将在堆栈跟踪中得到它。 我认为您真正想要的是CallStack。是相同的概念,但是异常中包含的跟踪实际上是“向后”排序的,因为异常会“向上”行进到处理它的地方。 尝试记录处理异常的CallStack。     

要回复问题请先登录注册