应用程序不包含我的 NuGet 包中的 NLog.config

Application doesn't include NLog.config from my NuGet package

我正在为我的团队设置一个 NuGet 包,供我们内部的所有项目使用。它包含用于为任何引用该包的项目配置 NLog 记录器的代码。然而,我们的自定义 NLog.config 文件没有出现在最终版本中,但 NuGet 包中包含的所有其他文件都在那里。

这是我的 nuspec 文件:

<?xml version="1.0"?>
    <package >
      <metadata>
        ...
      </metadata>
      <files>
        <file src="bin\Release\*.*" target="lib\net45" />    
      </files>
    </package>

我尝试将其添加到文件中,但它是多余的,并且没有任何改变:

<file src="bin\Release\NLog.config" target="lib\net45" />

由于我们包含了所有带有 *.* 的文件,它们都显示在 lib/net45 文件夹的 .nupkg 文件中。所有 .dll、.xml 和 .config 都在 nupkg 中,包括 NLog.config。所以我需要的文件就是把它放进包里就好了。

当我将此 NuGet 包包含在项目中并查看构建输出时,会出现问题。应用程序的构建输出包含我需要的 nupkg 中的所有内容,NLog.config 除外。甚至包括 Utilities.dll.config。

是否有需要在 nuspec 文件中指定的内容?还是在Visual Studio项目中设置MSBuild之类的?如何确保将 NuGet 包中包含的 所有 文件复制到应用程序的构建输出中?

您是否检查 nlog.config 文件属性?

如果文件属性 - "Copy to Output Directory" 是 "Do Not Copy",因此您无法在 Release 文件夹中看到配置文件。

此外,如果你想下载NLog.config文件到主目录,目标必须是空的。

就我而言,我在 nuspec 文件中使用了以下行: <file src="bin\Release\content\log4net.config" target=""></file>

我能够根据 Adam Catamak 的建议解决这个问题,并查看 NLog 自己的配置包的 nupkg 以了解他们是如何做到的。

适用于我的 nuspec 文件如下所示:

<?xml version="1.0"?>
<package >
  <metadata>
    ...
  </metadata>
  <files>
    <file src="bin\Release\*.*" target="lib\net45" />
    <file src="bin\Release\NLog.config" target="content"/>
    <file src="Install.ps1" target="tools"/>
  </files>
</package>

如果不使用Install.ps1,用户每次更新包时都需要手动更新为"Copy Always"。这是 NLog 使用的 Install.ps1:

param($installPath, $toolsPath, $package, $project)

$configItem = $project.ProjectItems.Item("NLog.config")

# set 'Copy To Output Directory' to 'Copy if newer'
$copyToOutput = $configItem.Properties.Item("CopyToOutputDirectory")
$copyToOutput.Value = 1

# set 'Build Action' to 'Content'
$buildAction = $configItem.Properties.Item("BuildAction")
$buildAction.Value = 2