我試圖在運行時獲取堆棧跟蹤信息。當從另一個文件夾加載PDB時StackTrace中缺少源文件信息
我用下面的代碼,這個工作在默認的項目配置罰款:
StackTrace trace = new System.Diagnostics.StackTrace(true);
StackFrame frame = trace.GetFrame(0);
int line = frame.GetFileLineNumber();
Console.WriteLine(line);
不過,我試圖來構建解決方案被配置爲把二進制文件和PDBS到單獨的文件夾:
<PropertyGroup>
...
<OutputPath>Binaries\$(Configuration)\bin\</OutputPath>
<IntermediateOutputPath>Binaries\$(Configuration)\obj\$(MSBuildProjectName)\</IntermediateOutputPath>
<PdbFile>Binaries\$(Configuration)\pdb\$(MSBuildProjectName).pdb</PdbFile>
<SkipCopyingSymbolsToOutputDirectory>true</SkipCopyingSymbolsToOutputDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
我可以在Visual Studio中的Modules
窗口中看到PDB已加載。但是,frame
變量不包含有關源文件(文件名,行和列)的信息。如果我將.pdb和.exe複製到同一個文件夾,則輸出再次正確。
因此,據我的理解:
- PDB是正確的(包含必要的信息),因爲它的工作原理,當我把文件複製到同一文件夾
- Visual Studio是能夠加載PDB,這樣的搜索路徑也應該是正確的
我已經在網上搜索(包括http://msdn.microsoft.com/en-us/library/ee416588.aspx和Display lines number in Stack Trace for .NET assembly in Release mode),但所有的文章主要集中在從正確位置加載PDBS(而據我而言,這不是 案件)。我不明白的是爲什麼沒有獲得源文件/行信息,雖然PDB被加載。
所以,問題是:當從不同位置加載.pdb時,是否可以獲取包含源文件信息的Stack Trace?如果不是,爲什麼?
哪個版本的Visual Studio和哪個版本的.Net? – OmegaMan
我使用的VS2012和目標。淨4.0 – nevermind