Getting Exception object in try..catch to enable FULL stacktrace, currently truncated it

can anyone help?

I try to get a full stack when in a try try..catch catch. It is currently truncated to only include the current method in which the error is ....

Let me explain .. Currently, my stack includes the "Third" method, where the error occurs, but First and Second are not included, I believe that 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 saw a few tricks to get the full stack in STRING, but the problem is that my logging platform expects an object of type Exception, but my variable (ex), which has my exception, is valid, but the StackTrace property is truncated.

In any case, I can get a FULL exception with a FULL stacktrace, so I can still send on my "EX", but this time it will have a UNtruncated stacktrace.

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

I would really appreciate any feedback.

+4
source share
4 answers
 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:

  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 so far. They all work, although Environment.StackTrace is the easiest. Here is the result:

 ==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) 
+2
source
 string fullStackTrace = exception.StackTrace + Environment.StackTrace; 

The current method can be added twice, if so, delete one line from one of the properties.

+3
source

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 out this article for more information: StackStace Class

Hope this helps.

+1
source

You do not understand how stack tracing works. What is included in the exception trace is a “route” short to indicate that the exception has reached the point at which you are handling it.

Thus, in your example, he has not traveled yet; he is still in the “third” method. If it started before the Second, then it would move to the third, to the second, and you will get it on the stack track.

I think you really want CallStack . This is the same concept, but the trace included in the exception actually includes a “reverse” sort to talk, as the exception moves up to where it is being processed.

Try registering CallStack where you are handling the exception.

+1
source

All Articles