Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Using Environment.StackTrace

Discussion in 'Scripting' started by NCarter, Aug 2, 2006.

  1. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    I'm trying to debug some quite complex C# code at the moment, and I'm in a situation in which I'd like to detect where a method is being called from. I could go through my code and add print statements before every invocation, but it seems to me that it should be possible to do something a bit smarter.

    After a bit of poking around in Google, I found the Environment.StackTrace feature, which looks like it'd do exactly what I want - to put the current status of the stack in a string which I can print. Unfortunately, it doesn't seem to work. The following code:

    Code (csharp):
    1. Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
    ..just prints StackTrace: ''

    I presume this is a Mono problem rather than a Unity problem, but I'm wondering if there's either some way of making it work, or if anyone could suggest an alternative way of getting similar information.
     
  2. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,648
    from the mono page...

    Code (csharp):
    1.   public void MyMethod3 () {
    2.         Console.WriteLine("TestCallStack: {0}",
    3.                           Environment.StackTrace);
    4.     }
    there's no ' ' around {0}. maybe?
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,965
    You can use the one we use for extracting stacktraces in various situations:
    Code (csharp):
    1.  
    2. string str = UnityEngine.StackTraceUtility.ExtractStackTrace ();
    3.  
    Depending on what you want, you might just want to call Debug.Log.

    That prints the whole stacktrace to the UnityConsole too. Just select it in the console and take a look at the view in the bottom.
     
    Tony_Max, IgorAherne and rigidbuddy like this.
  4. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    Funny, that just returns an empty string for me, too....

    Thanks, that turned out to be quite sufficient for this purpose.
     
unityunity