How to get the linenumber from a stackframe?

Hello,
i want to trace what my app is doing.

so i made a short debug logger

Code (CSharp):

    static public void prinstantiate(params string[] mytuf) {
        if (!tmpflowlog) return;
        System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace();           // get call stack
        System.Diagnostics.StackFrame[] stackFrames = stackTrace.GetFrames();  // get method calls (frames)
                                                                               // write call stack method names
        string display="";
        foreach (string mtuf in mytuf) {
            display += mtuf;
        }
        string text= "

Instantiate: " + display + " Count: “+ profiler +” on " + g.stopWatch.Elapsed+"
“;
foreach (System.Diagnostics.StackFrame stackFrame in stackFrames) {
text+= " " + stackFrame.GetMethod ().Name+ " L” +stackFrame.GetFileLineNumber() + " : ";
}
File.AppendAllText (Path.Combine (p.logpath, “instantiate.txt”), text);

        //Debug.Log (text);
        profiler++;
    }

but i cannot get the lines with it prints something like:

Instantiate: infoinput Count: 1825 on 00:00:10.4046821
prinstantiate L0 : getobjbn L0 : .cctor L0 : hover L0 : hover L0 : hover L0 : Update L0 :

Question 1:
How can i get the linenumber?

Question2:
Why does it return 0?

You forgot a parameter to the c’tor :slight_smile:

System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(true);

The optional boolean parameter specifies that we want to gather source related info such as file names and line numbers.

It is false by default.