C# 使用configSource转换包含的配置文件
这个问题有点像两个部分。在VS2015中,我的MVC项目有多个不同的构建配置,测试、UAT、Live等。使用我的C# 使用configSource转换包含的配置文件,c#,asp.net-mvc,visual-studio,web-config,web-config-transform,C#,Asp.net Mvc,Visual Studio,Web Config,Web Config Transform,这个问题有点像两个部分。在VS2015中,我的MVC项目有多个不同的构建配置,测试、UAT、Live等。使用我的web.config我只需右键单击它并选择添加配置转换,即可为每个构建配置创建转换文件 如果我有一个外部配置文件,比如Log4Net.config,我如何将其配置为具有类似web.config的依赖转换?这是通过编辑project.csproj文件手动完成的吗 其次,我有一个web.config文件,因此: <configuration> <configSec
web.config
我只需右键单击它并选择添加配置转换,即可为每个构建配置创建转换文件
如果我有一个外部配置文件,比如Log4Net.config
,我如何将其配置为具有类似web.config
的依赖转换?这是通过编辑project.csproj
文件手动完成的吗
其次,我有一个web.config
文件,因此:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Log4net" />
</configSections>
...
<log4net configSource="Log4Net.config" />
</configuration>
如何使用相同的配置转换来转换包含的
Log4Net.config
文件?我意识到我可以将另一个TransformXml
放入后构建目标,但这是执行此转换的正确方法,还是我遗漏了什么?我选择了使用基本Log4Net.config
文件的解决方案,每个构建配置的Log4Net.XXX.config
文件和AfterBuild
目标中的额外TransformXml
任务:
- 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.config
文件。它使用与转换web.config
文件相同的方法,因此任何其他开发人员都可以轻松理解该项目
虽然这是可行的,并且已经投入生产一段时间了,但我仍在寻找一些确认,证明这是转换包含的配置文件的正确方法。我也有同样的问题。你设法解决了吗?@Matiascero我在后构建目标中使用了额外的TransformXml解决了这个问题。我在下面的回答中概述了这一点。嗯。
<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>
<?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>