How to Get a Stack Trace In. Net in Normal Execution?

In VB .NET, I know I can get a stack trace by looking at the value of ex.StackTrace when handling an exception. How can I get the functions on the stack when I am not handling an exception? I am looking to implement a logging system of some sort to record the steps the user takes prior to a crash to assist in debugging.

4 Answers

Environment.StackTrace gives you a string, but for more detailed information and options, use the StackTrace class.

To be more specific, check out the constructor options:

If you need the stack trace starting at the caller's frame (e.g. in a logging function: you don't want everything to start with MyLogMethod), you should try this one, which takes an int parameter, the number of frames to skip.

If you need the stack trace without source information (e.g. if you don't want to give away information about your source code), try this one:

Hope that helps!

System.Environment.StackTrace

You can also use these calls to get more detailed information such as GetILOffset, GetNativeOffset or GetType:

Import System.Diagnostics
            
Debug.WriteLine("Call Stack:")
Dim stackTrace As StackTrace = New StackTrace()         
Dim stackFrames() As StackFrame = stackTrace.GetFrames() 

For Each stackFrame As StackFrame In stackFrames
    Debug.WriteLine(stackFrame.GetMethod().Name)
Next

See

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest