StackTrace filename unknown

2019-02-05 16:46发布

Something strange is happening in my code where I'm using a StackTrace. It's almost as if the debug info is not being loaded... but I'm running this on the DEBUG build.The .pdb files are definitelly in the bin directory and up to date. I've seriously ran out of ideeas :

public class TraceHelper
{
    private static IDictionary<string,int> TraceDictionary = new Dictionary<string,int>();

    public TraceHelper(int duration)
    {

         ...
        TraceDictionary[InternalGetCallingLocation()]+=duration;
         ... 

    }
    public static string InternalGetCallingLocation ()
    {
          var trace = new System.Diagnostics.StackTrace();
          var frames = trace.GetFrames();
          var filename = frames[1].GetFileName(); //<<-- this always returns null
          return frames[0].ToString(); //this returns:
          //  "InternalGetCallingLocation at offset 99 in file:line:column <filename unknown>:0:0"
    }
}

1条回答
一夜七次
2楼-- · 2019-02-05 17:24

A bit more googling arround found this post

Turns out StackTrace has a special constructor

public StackTrace(bool fNeedFileInfo) 

if you need file info to be populated. Ouch

查看更多
登录 后发表回答