生产
最简单的配置方法是有一个Directory.Build.props
具有类似这样的通用参数......
<Project>
<PropertyGroup>
<!-- Based on https://devblogs.microsoft.com/dotnet/producing-packages-with-source-link/ -->
<!-- Publish the repository URL in the built .nupkg (in the NuSpec <Repository> element) -->
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<!-- Embed source files that are not tracked by the source control manager in the PDB -->
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<!-- Embed symbols containing Source Link in the main file (exe/dll) -->
<DebugType>embedded</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(TF_BUILD)' == 'true'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
</PropertyGroup>
<Project>
在生成包的每个项目中,您可以引用适当的SourceLink
为您的系统组装。
解释如下:
-
持续集成构建:使你的 CI 系统产生
文件的规范路径,只能为 CI 服务器设置为
否则你将无法找到本地源 DebugType
-
生成文档文件:生成 XML 文档文件以包含在 dll 中 - 您确实编写了文档,不是吗 ;-)
-
Embedded:将pdb数据添加到汇编文件中,简化
以增加 dll 大小为代价进行分发
更多信息请参见这里微软博客文章 https://devblogs.microsoft.com/dotnet/producing-packages-with-source-link/
消费
NET 7
将符号文件复制到执行目录现在已合并到NET 7.0 SDK/VS2022 17.4+ https://github.com/dotnet/sdk/issues/22362作为一种选择加入的方法。将以下片段添加到您的项目文件中...
<PropertyGroup>
...
<CopyDebugSymbolFilesFromPackages>true</CopyDebugSymbolFilesFromPackages>
<CopyDocumentationFilesFromPackages>true</CopyDocumentationFilesFromPackages>
</PropertyGroup>
这会将所有符号和文档文件复制到您的输出目录中;如果您只想要一个子集,例如your文件,您将必须在此基础上实现过滤机制
Earlier
如果您使用的是 VS2017 15.4 或更高版本,您可以在项目文件中定义 MSBuild 属性
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
这在中讨论NuGet #4142 https://github.com/NuGet/Home/issues/4142#issuecomment-341867400
但是,仍然存在一个问题,因为新项目系统不会将 pdb 从包复制到 bin/publish 文件夹中.NET核心3.0+ https://github.com/dotnet/sdk/issues/1458,一个很好的总结也位于来源链接/#628 https://github.com/dotnet/sourcelink/issues/628
由于该修复仅在 .NET 7 SDK+ 中受支持,因此您需要一种解决方法,即将以下片段包含到 API 和测试项目中,以确保您拥有适当的 pdb 以允许您单步执行远程源代码。
<!-- https://github.com/dotnet/sdk/issues/1458#issuecomment-1063915490 -->
<Target Name="IncludeSymbolFiles" AfterTargets="ResolveAssemblyReferences" Condition="@(ReferenceCopyLocalPaths) != ''">
<ItemGroup>
<ReferenceCopyLocalPaths Include="%(ReferenceCopyLocalPaths.RelativeDir)%(ReferenceCopyLocalPaths.Filename).pdb; %(ReferenceCopyLocalPaths.RelativeDir)%(ReferenceCopyLocalPaths.Filename).xml" />
<ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="!Exists('%(FullPath)')" />
</ItemGroup>
</Target>