Getting the Exception object in a try..catch to include FULL stacktrace, currently its truncated

99封情书 提交于 2019-12-05 17:38:05
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)
string fullStackTrace = exception.StackTrace + Environment.StackTrace;

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

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 : StackStace class

Hope this helps.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!