Having good error logging in production is essential for effective analysis and fixing of runtime errors. The stack trace inducing line numbers is a very helpful element of error log. One 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 PDB files are not usually included in deployment packages. Ordinarily, PDB files included in referenced NuGet packages are not copied to the output folder or included in the deployment package. However, including PDB files from assembly references in deployment packages is actually quite easy.
All it takes is to dynamically add the desired PDB to the list of ‘content’ files. For example, the easiest 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 I will 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’.
In short, now when you publish your project, all PDB files will be included.
Thanks. It really helps me.