在 try..catch 中获取 Exception 对象以包含完整的堆栈跟踪,目前它已被截断

发布于 2024-11-09 06:56:36 字数 690 浏览 0 评论 0原文

我试图在 try..catch 的 catch 中获取完整的堆栈跟踪。目前它已被截断为仅包含出现错误的当前方法。

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

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 中获取完整堆栈跟踪的技巧,但问题是我的日志记录框架需要一个“Exception”类型的对象,但我的异常变量(ex)是有效的,但属性 StackTrace 被截断。

无论如何,我是否可以收到带有完整堆栈跟踪的完整异常,这样我仍然可以发送我的“EX”,但这次它将有一个未截断的堆栈跟踪。

UnhandledError 事件似乎工作得就像我到达这里一样,异常有一个堆栈跟踪并且已完全填充。

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.

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!
    }
}

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.

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 event seems to work as if I arrive here the Exception has a stack trace and is fully populated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

护你周全 2024-11-16 06:56:36
string fullStackTrace = exception.StackTrace + Environment.StackTrace;

当前方法可能会添加两次,如果是这样,请从属性之一中删除一行。

string fullStackTrace = exception.StackTrace + Environment.StackTrace;

The current method may be added two times, if so, remove one line from one of the properties.

遇见了你 2024-11-16 06:56:36
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)
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

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)
妥活 2024-11-16 06:56:36

您不了解堆栈跟踪如何工作。异常跟踪中包含的是“路线”,简而言之,异常已到达您正在处理它的位置。

因此,在您的示例中,它根本还没有移动,它仍然处于“第三”方法中。如果它会冒泡到“第二”,那么它会穿过第三,到达第二,并且您将在堆栈跟踪中得到它。

我认为您真正想要的是 CallStack。是相同的概念,但是异常中包含的跟踪实际上是“向后”包含的,因为异常“向上”移动到处理它的位置。

尝试记录处理异常的 CallStack。

You don't understand how stack trace work. What is included in an exception trace is the "route" short to speak that the exception has traveled to reach the point where you're handling it.

Thus, on your example, it hasn't traveled at all yet, it's still in the "Third" method. If it would bubble up to "Second", then it would have gone across Third, to Second, and you'll be getting that on the stack trace.

I think what you actually want is the CallStack. Is the same concept, but the trace that's included in the exception is actually included "backwards" sort to speak, as the exception travels "up" to the place where it is handled.

Try logging the CallStack where you handle the exception.

梦幻的味道 2024-11-16 06:56:36

您需要这样的东西:

        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());
            }
        }
    }

查看这篇文章以获取更多信息:
StackTrace 类

希望这会有所帮助。

You need something like this :

        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());
            }
        }
    }

Check this article for more info :
StackTrace class

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文