StackTrace文件名未知

Rad*_*094 23 c# asp.net reflection stack-trace

在我使用StackTrace的代码中发生了一些奇怪的事情.这几乎就像没有加载调试信息一样......但是我在DEBUG构建中运行它..pdb文件在bin目录中是最新的并且是最新的.我已经严重失去了思想:

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"
    }
}
Run Code Online (Sandbox Code Playgroud)

Rad*_*094 50

更多谷歌搜索发现这篇文章

结果StackTrace有一个特殊的构造函数

public StackTrace(bool fNeedFileInfo) 
Run Code Online (Sandbox Code Playgroud)

如果您需要填充文件信息.哎哟

  • 犯规!犯规!犯规! (4认同)