使用configSource转换包含的配置文件 [英] Transforming included configuration files using configSource
问题描述
这个问题有点像是两个伙伴.在VS2015中,我的MVC项目具有多个不同的构建配置,包括测试,UAT,实时等.使用我的web.config
,只需右键单击它,然后选择 Add Config Transform 即可为每个构建配置创建转换文件
This question is kind of a two parter. In VS2015 my MVC project has multiple different build configurations, Test, UAT, Live etc. With my web.config
I can simply right click it and select Add Config Transform to create the transform files for each build configuration.
如果我有一个外部配置文件(例如Log4Net.config
),如何配置它以具有像web.config
这样的相关转换?是通过编辑project.csproj
文件手动完成的吗?
If I have an external config file, such as Log4Net.config
how can I configure this to have dependant transforms like web.config
? Is this done manually by editing the project.csproj
file ?
第二,因此我有一个web.config
文件:
Secondly, I have a web.config
file thus :
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Log4net" />
</configSections>
...
<log4net configSource="Log4Net.config" />
</configuration>
构建项目时,web.config
会自动通过project.csproj
文件中的以下AfterBuild目标进行转换:
When I build the project, the web.config
automatically gets transformed through the following AfterBuild target in the project.csproj
file :
<Target Name="AfterBuild">
<TransformXml Source="Web.config"
Transform="Web.$(Configuration).config"
Destination="$(OutputPath)\$(AssemblyName).config" />
</Target>
如何使用相同的配置转换来转换包含的Log4Net.config
文件?我意识到可以将另一个TransformXml
放入AfterBuild目标中,但这是进行此转换的正确方法,还是我错过了什么?
How can I transform the included Log4Net.config
file using the same configuration transformation ? I realise that I could place another TransformXml
into the AfterBuild target, but is this the correct way of doing this transform, or am I missing something ?
推荐答案
我选择了使用基本Log4Net.config
文件,用于每个构建配置的Log4Net.XXX.config
文件以及在AfterBuild
目标:
I opted for the solution of using a base Log4Net.config
file, a Log4Net.XXX.config
file for each build configuration and an additional TransformXml
task in the AfterBuild
target :
- Log4Net.config
- Log4Net.Debug.config
- Log4Net.Release.config
- Log4Net.Test.config
- Log4Net.UAT.config
project.csproj
文件现在看起来像这样:
The project.csproj
file now looks like this :
<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的日志记录级别):
And an example Log4Net.Test.config
looks like this (I am using the transform to change connection strings and Log4Net's logging level) :
<?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
文件相同的方法,因此,选择该项目的任何其他开发人员都应该易于理解.
This transforms the Log4Net.config
file in the output path successfully. It uses the same approach as transforming web.config
files and so should be easily understandable by any other developer that picks up the project.
尽管这可行并且已经投入生产了一段时间,但我仍在寻找一些确认,证明这是对包含的配置文件进行转换的正确方法.
Whilst this works and has been in production for a while now, I'm still looking for some confirmation that this is the correct way of doing transforms of included config files.
这篇关于使用configSource转换包含的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!