Having good error logging in production is essential for effective analysis and fixing of runtime errors. On very helpful element of error log is the stack trace, including line numbers. Unfortunately, most of the line numbers are missing in stack traces from production. This is because usually PDB files are not included in deployment packages. Usually, PDB files included in referenced NuGet packages are not copied to the output folder or included in the deployment package. Including PDB files from assembly references in deployment packages is quite easy.
All it takes is to dynamically add the desired PDB to the list of ‘content’ files. The most easy way to do this is in the CSPROJ file itself:
<Target Name="IncludePdbInPackage" BeforeTargets="PrepareForPublish"> <ItemGroup> <_PdbFiles Include="@(ReferencePath->Replace('.dll', '.pdb'))" /> <ContentWithTargetPath Include="@(_PdbFiles->'%(FullPath)')" RelativePath="%(_PdbFiles.Identity)" TargetPath="%(_PdbFiles.Filename)%(_PdbFiles.Extension)" CopyToPublishDirectory="Always" Condition="Exists('%(_PdbFiles.RootDir)%(_PdbFiles.Directory)%(_PdbFiles.Filename)%(_PdbFiles.Extension)')"/> </ItemGroup> </Target>
Most of the code above is self-explanatory, I guess. One thing to mention is the item ‘ReferencePath’. This contains a list of all referenced assemblies. The code above replaces the string “.dll” with “.pdb” and stores these file paths in an item named ‘_PdbFiles’. Next, each item in ‘_PdbFiles” that actually exists gets added to ‘ContentWithTargetPath’.
That’s it. If you publish your project, the all PDB files will be included.