将Exception对象放在try..catch中以包含FULL stacktrace,目前它被截断 [英] Getting the Exception object in a try..catch to include FULL stacktrace, currently its truncated

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

问题描述

任何人都可以帮助?



我正在尝试在try..catch的捕获中获取完整的堆栈跟踪。目前,它被截断,仅包括当前的方法,其中的错误是....



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

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

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

private void Third()
{
try
{
throw new SystemException(ERROR HERE!);
}
catch(Exception ex)
{
//我将在这里登录EXCEPTION对象EX!但是exStackTrace被截断!
}
}

我看到了一些技巧,堆栈跟踪在一个STRING但问题是我的日志框架期待一个类型为异常的对象,但我的变量(ex)有我的例外是有效的但是属性StackTrace被截断。



有没有办法,我可以收到一个完整的异常与一个完整的堆栈跟踪,所以我能够仍然发送我的EX,但这次它将有一个UNTruncated堆栈跟踪。



我真的非常感谢任何反馈..

未处理的Errror事件似乎工作,就像我到达这里异常有一个堆栈跟踪和完全填充...

解决方案

  class Program 
{
static void Main ] 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!);
}
}

这种情况下的输出是:

在C:\Temp\customers\stack\Program.cs:行37 $ b $ stack $ b at stack.Program.Second()in C:\Temp\customers\stack\Program.cs:line 31
at stack.Program.First()in C:\Temp\ customers \stack\Program.cs:行25
在stack.Program.Main(String [] args)在C:\Temp\customers\stack\Program.cs:li
ne 14

更新:我突然怀疑,迄今提出的解决方案。他们都工作,虽然Environment.StackTrace是最简单的。这是输出如下所示:

  == ex.StackTrace == 
at stack.Program.Third()在C:\Temp\customers\stack\Program.cs:line 35

== Environment.StackTrace per Guillaume / JorgeCórdoba==
at stack.Program.Third ()在C:\Temp\customers\stack\Program.cs:第35行在S
ystem.Environment.GetStackTrace(Exception e,Boolean needFileInfo)
在System.Environment.get_StackTrace ()
在stack.Program.Third()在C:\Temp\customers\stack\Program.cs:行44
在stack.Program.Second()在C:\\ C:\Temp\customers\stack\Program.cs:line 21
at stack.Program.Main(String [] args)在C:\Temp\customers\stack\Program.cs:li
ne 15
在System.AppDomain._nExecuteAssembly( Assembly Assembly,String [] args)
在System.AppDomain.Exec uteAssembly(String assemblyFile,Evidence assemblySec
urity,String [] args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,C
ontextCallback回调,对象状态)
在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():(第52行)
Void Second():(第27行)
Void First():(第21行)
void主(System.String [ ]):(第15行)
Int32 _nExecuteAssembly(System.Reflection.Assembly,System.String []):(行0)
Int32 ExecuteAssembly(System.String,System.Security.Policy.Evidence, System.Str
ing []):(行0)
Void RunUsersAssembly():(行0)
Void ThreadStart_Context(System.Object):(行0)
void Run(System.Threading.ExecutionContext,System.Threading.ContextCallback,Sy
stem.Object):(行0)
Void ThreadStart():(行0)


can anyone help?

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

Let me explain.. Currently i my stacktrace 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 stacktrace 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 stacktrace so i am able to still send along my "EX" but this time it will have an UNtruncated stacktrace.

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

I would really appreciated any feedback..

解决方案

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)

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

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