【发布时间】:2013-11-25 18:57:19
【问题描述】:
我正在尝试在运行时获取堆栈跟踪信息。
我正在使用以下代码,这在默认项目配置中运行良好:
StackTrace trace = new System.Diagnostics.StackTrace(true);
StackFrame frame = trace.GetFrame(0);
int line = frame.GetFileLineNumber();
Console.WriteLine(line);
但是,我正在尝试构建的解决方案被配置为将二进制文件和 PDB 放到单独的文件夹中:
<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),但所有文章主要关注从正确位置加载 PDB(就我而言,情况并非如此)。我不明白的是为什么虽然加载了PDB,却没有获得源文件/行信息。
所以,问题是:当 .pdb 从不同位置加载时,是否可以获得包含有关源文件信息的堆栈跟踪?如果不是,为什么?
【问题讨论】:
-
哪个版本的 Visual Studio 和哪个版本的 .Net?
-
我正在使用 VS2012 并针对 .Net 4.0
标签: c# .net visual-studio debugging debug-symbols