MrD*_*per 7 c# asp.net-mvc web-config visual-studio web-config-transform
这个问题有点像两个人.在VS2015中,我的MVC项目有多种不同的构建配置,Test,UAT,Live等.web.config我可以直接右键单击它并选择Add Config Transform为每个构建配置创建转换文件.
如果我有一个外部配置文件,例如Log4Net.config如何配置它以具有相关的转换web.config?这是通过编辑project.csproj文件手动完成的吗?
其次,我有一个web.config文件:
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, Log4net" />
</configSections>
...
<log4net configSource="Log4Net.config" />
</configuration>
Run Code Online (Sandbox Code Playgroud)
当我构建项目时,会web.config自动通过project.csproj文件中的以下AfterBuild目标进行转换:
<Target Name="AfterBuild">
<TransformXml Source="Web.config"
Transform="Web.$(Configuration).config"
Destination="$(OutputPath)\$(AssemblyName).config" />
</Target>
Run Code Online (Sandbox Code Playgroud)
如何Log4Net.config使用相同的配置转换转换包含的文件?我意识到我可以将另一个TransformXml放入AfterBuild目标中,但是这是进行此转换的正确方法,还是我错过了什么?
我选择了使用基本Log4Net.config文件,Log4Net.XXX.config每个构建配置的文件以及目标中的其他TransformXml任务的解决方案AfterBuild:
该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>
Run Code Online (Sandbox Code Playgroud)
一个示例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>
Run Code Online (Sandbox Code Playgroud)
这会Log4Net.config成功转换输出路径中的文件.它使用与转换web.config文件相同的方法,因此任何其他开发人员都可以轻松理解它.
虽然这有效并且已经生产了一段时间了,但我仍然在寻找一些确认,这是对包含的配置文件进行转换的正确方法.
| 归档时间: |
|
| 查看次数: |
2303 次 |
| 最近记录: |