在try..catch中获取Exception对象以包含完整的stacktrace,目前已将其截断 [英] Getting the Exception object in a try..catch to include full stacktrace, currently it's truncated

查看:145
本文介绍了在try..catch中获取Exception对象以包含完整的stacktrace,目前已将其截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在try..catch的范围内时,我试图获取完整的堆栈跟踪。目前,它被截断为仅包含错误所在的当前方法。

I am trying to get the full stack trace when within a catch of try..catch. Currently its truncated to include only the current method where the error is.

让我解释一下。当前,我的堆栈跟踪包含发生错误的方法 Third,但不包括第一和第二,我相信这是设计使然。

Let me explain. Currently my stack trace includes the method "Third" where the error happens but First and Second isn't included, I believe this is by design.

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中进行堆栈跟踪,但问题是我的日志记录框架期望对象的类型为异常,但是具有我的异常的变量(ex)有效,但是属性StackTrace被截断了。

I have seen a number of tricks to get the full stack trace in a STRING but problem is my logging framework expects a object of type "Exception", but my variable (ex) which has my exception is valid but the property StackTrace is truncated.

无论如何,我可以通过FULL堆栈跟踪接收到FULL异常,因此我仍然可以发送 EX,但是这次它将具有Untruncated堆栈跟踪。

Is there anyway I can receive a FULL exception with a FULL stack trace so I am able to still send along my "EX" but this time it will have an Untruncated stack trace.

UnhandledErrror事件似乎可以正常工作,就像我到达此处一样,该异常具有堆栈跟踪并已完全填充。

UnhandledErrror event seems to work as if I arrive here the Exception has a stack trace and is fully populated.

推荐答案

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!");
        }
    }

在这种情况下,输出为:

The output in this case is:

   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是最简单的。输出结果如下:

Update: I suddenly had doubts, and ran through all the solutions presented thus far. They all work, though Environment.StackTrace is the easiest. Here's what the output looks like:

==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)

这篇关于在try..catch中获取Exception对象以包含完整的stacktrace,目前已将其截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆