448

I have a regular C# code. I have no exceptions. I want to programmatically log the current stack trace for debugging purpose. Example:

public void executeMethod() 
{
    logStackTrace();
    method();
}

7 Answers 7

513

Have a look at the System.Diagnostics namespace. Lots of goodies in there!

System.Diagnostics.StackTrace t = new System.Diagnostics.StackTrace();

This is really good to have a poke around in to learn what's going on under the hood.

I'd recommend that you have a look into logging solutions (Such as NLog, log4net or the Microsoft patterns and practices Enterprise Library) which may achieve your purposes and then some.

3
  • 98
    Keep in mind that StackTrace is dog slow - so use it sparingly. Sep 19, 2011 at 8:31
  • 9
    I think your answer should mention that you can use t.ToString() to get a string representation of the stack trace, which is probably what the OP wanted.
    – Sasino
    Sep 12, 2022 at 18:18
  • 9
    If someone want File Name and File line numbers. You must at least call new System.Diagnostics.StackTrace(true).
    – David Raab
    Sep 13, 2022 at 7:55
273

An alternative to System.Diagnostics.StackTrace is to use System.Environment.StackTrace which returns a string-representation of the stacktrace.

Another useful option is to use the $CALLER and $CALLSTACK debugging variables in Visual Studio since this can be enabled run-time without rebuilding the application.

7
  • 8
    Environment.StackTrace just new's up an instance of StackTrace.
    – Daniel
    Feb 16, 2012 at 22:40
  • 12
    @Daniel: Yes, but System.Environment.StackTrace might be a more convenient way of accessing that information.
    – larsmoa
    Feb 17, 2012 at 19:47
  • 6
    @AndreiRinea: Actually, I belive you can access line numbers using System.Diagnostics.StackTrace - see msdn.microsoft.com/en-us/library/…
    – larsmoa
    May 13, 2013 at 11:05
  • 13
    Isn't it annoying that Environment.StackTrace always starts out with at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo) at System.Environment.get_StackTrace()? That's not part of the current stack trace as at the point that someone's looking for it.
    – Rory
    Apr 20, 2016 at 22:43
  • 15
    @Rory, have a look at the constructor StackTrace(int skipFrames, bool fNeedFileInfo), you can skip the starting frame. ipen the method in ILSpy and you can see what it does Sep 16, 2018 at 12:31
40

There are two ways to do this. The System.Diagnostics.StackTrace() will give you a stack trace for the current thread. If you have a reference to a Thread instance, you can get the stack trace for that via the overloaded version of StackTrace().

You may also want to check out Stack Overflow question How to get non-current thread's stacktrace?.

1
  • Error CS1955 Non-invocable member 'StackTrace' cannot be used like a method.
    – David
    Oct 23, 2021 at 23:20
18

You can also do this in the Visual Studio debugger without modifying the code.

  1. Create a breakpoint where you want to see the stack trace.
  2. Right-click the breakpoint and select "Actions..." in VS2015. In VS2010, select "When Hit...", then enable "Print a message".
  3. Make sure "Continue execution" is selected.
  4. Type in some text you would like to print out.
  5. Add $CALLSTACK wherever you want to see the stack trace.
  6. Run the program in the debugger.

Of course, this doesn't help if you're running the code on a different machine, but it can be quite handy to be able to spit out a stack trace automatically without affecting release code or without even needing to restart the program.

2
  • 8
    If you want to see a stack trace and you're already in the VS debugger at a breakpoint, just go to Debug->Windows->Call Stack.
    – khargoosh
    Dec 16, 2015 at 22:45
  • 6
    Yes, but if you do it this way, the program doesn't have to stop for you to see the stack trace. Mar 3, 2016 at 23:50
17
Console.WriteLine(
    new System.Diagnostics.StackTrace().ToString()
    );

The output will be similar to:

at YourNamespace.Program.executeMethod(String msg)

at YourNamespace.Program.Main(String[] args)

Replace Console.WriteLine with your Log method. Actually, there is no need for .ToString() for the Console.WriteLine case as it accepts object. But you may need that for your Log(string msg) method.

7

This worked for me:

Debug.WriteLine(Environment.StackTrace);
-7
   private void ExceptionTest()
    {
        try
        {
            int j = 0;
            int i = 5;
            i = 1 / j;
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            var stList = ex.StackTrace.ToString().Split('\\');
            Console.WriteLine("Exception occurred at " + stList[stList.Count() - 1]);
        }
    }

Seems to work for me

2
  • 8
    This is just generating an obvious exception. Why? Why not simply explicitly instantiate an exception, rather than a flaw that will raise an exception in and of itself? What's the benefit of the additional catch logic required to get to the actual exception? And even then, you're still ignoring the actual posted question on how to get a stack trace without any exception (read the title).
    – Flater
    Jun 6, 2018 at 6:17
  • it's a valid answer, but a god awful solution. NOONE SHOULD USE IT... @Jeff wherever you done that, replace it with a descent code like the other answers suggested.
    – Tomer W
    Jun 29, 2018 at 6:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.