6

这个问题是一种双亲。在VS2015中,我的MVC项目有多种不同的构建配置,测试,UAT,Live等。通过我的web.config,我可以简单地右键单击它并选择添加配置变换为每个构建配置创建转换文件。使用configSource转换包含的配置文件

如果我有一个外部配置文件,如Log4Net.config我怎样才能配置这个依赖转换如web.config?这是通过编辑project.csproj文件手动完成的吗?

其次,我有一个web.config文件这样的:

<configuration> 
    <configSections> 
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Log4net" /> 
    </configSections> 

    ... 

    <log4net configSource="Log4Net.config" /> 
</configuration> 

当我生成项目,该web.config自动获得通过以下AfterBuild目标转化在project.csproj文件:

<Target Name="AfterBuild"> 
    <TransformXml Source="Web.config" 
      Transform="Web.$(Configuration).config" 
      Destination="$(OutputPath)\$(AssemblyName).config" /> 
</Target> 

哪有我使用相同的配置转换来转换包含的Log4Net.config文件?我意识到我可以将另一个TransformXml放置到AfterBuild目标中,但这是做这种转换的正确方法,还是我错过了某些东西?

+0

我遇到同样的问题。你设法解决它吗? –

+0

@MatiasCicero我已经在AfterBuild目标中使用额外的TransformXml解决了它。我在下面的答案中概述了它。 HTH。 – MrDeveloper

回答

3

我选择使用碱Log4Net.config文件的溶液,Log4Net.XXX.config文件对于每个生成配置和附加TransformXml任务在AfterBuild目标:

  • Log4Net.config
  • Log4Net.Debug.config
  • Log4Net.Release.config
  • Log4Net.Test.config
  • Log4Net.UAT.config

project.csproj文件现在看起来是这样的:

<Content Include="Log4Net.config"> 
    <CopyToOutputDirectory>Always</CopyToOutputDirectory> 
</Content> 
<None Include="Log4Net.Debug.config"> 
    <DependentUpon>Log4Net.config</DependentUpon> 
</None> 
<None Include="Log4Net.Release.config"> 
    <DependentUpon>Log4Net.config</DependentUpon> 
</None> 
<None Include="Log4Net.Test.config"> 
    <DependentUpon>Log4Net.config</DependentUpon> 
</None> 
<None Include="Log4Net.UAT.config"> 
    <DependentUpon>Log4Net.config</DependentUpon> 
</None> 

.... 

<Target Name="AfterBuild"> 
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="$(OutputPath)\$(AssemblyName).config" /> 
    <TransformXml Source="Log4Net.config" Transform="Log4Net.$(Configuration).config" Destination="$(OutputPath)\Log4Net.config" /> 
</Target> 

和示例Log4Net.Test.config看起来像这样(我用的变换来改变连接字符串和log4net的的日志记录级别):

<?xml version="1.0" encoding="utf-8"?> 

<log4net xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <appender> 
     <connectionString 
      value="Data Source=example.com;Initial Catalog=ExampleLogs;User ID=xxx;Password=xxx" 
      xdt:Transform="Replace" /> 
    </appender> 

    <root> 
     <level 
      value="DEBUG" 
      xdt:Transform="Replace" /> 
    </root> 
</log4net> 

这将成功转换输出路径中的Log4Net.config文件。它使用与转换web.config文件相同的方法,因此任何其他开发人员都应该易于理解该项目。

虽然这个工作,并已生产了一段时间,我仍然在寻找一些确认,这是做包括配置文件转换的正确方法。

相关问题