NLog.config 转型

NLog.config Transformation

我有一个 NLog.config 文件,我想在发布我的网站之前对其进行转换。类似于 web.config 的转换方式。我该如何做到这一点?我找不到有关如何执行此操作的任何可靠资源。

我尝试向 csproj 添加一个转换

  <Target Name="BeforeBuild" Condition="exists('NLog.$(Configuration).config')">
    <Message Text="Tranforming NLog..."/>  
    <TransformXml Source="NLog.config" Transform="NLog.$(Configuration).config" Destination="$(OutputPath)\NLog.config" />
  </Target>

还将 NLog 添加到 csproj:

   <Content Include="NLog.config">
      <SubType>Designer</SubType>
    </Content>
    <None Include="NLog.aws-prod.config">
      <DependentUpon>NLog.config</DependentUpon>
    </None>
    <None Include="NLog.aws-test.config">
      <DependentUpon>NLog.config</DependentUpon>
    </None>

但这不会将转换后的 NLog.config 复制到包目录(或部署到 AWS 时)。原始 NLog.config 被复制,并在 /bin 目录中复制一份。

SlowCheetah 似乎在做我想做的事。我试过了,我对我的 csproj 进行了更改以添加:

<TransformOnBuild>true</TransformOnBuild>

<IsTransformFile>True</IsTransformFile>

所以最后的变化是这样的:

<Content Include="NLog.config">
  <TransformOnBuild>true</TransformOnBuild>
  <SubType>Designer</SubType>
</Content>
<None Include="NLog.aws-prod.config">
  <DependentUpon>NLog.config</DependentUpon>
  <IsTransformFile>True</IsTransformFile>
  <SubType>Designer</SubType>
</None>
<None Include="NLog.aws-test.config">
  <DependentUpon>NLog.config</DependentUpon>
  <IsTransformFile>True</IsTransformFile>
</None>

就这样,NLog.config变身了!!不需要以下目标:

<Target Name="BeforeBuild" Condition="exists('NLog.$(Configuration).config')">
    <Message Text="Tranforming NLog..."/>  
    <TransformXml Source="NLog.config" Transform="NLog.$(Configuration).config" Destination="$(OutputPath)\NLog.config" />
  </Target>