Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming included configuration files using configSource

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.

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 ?

Secondly, I have a web.config file thus :

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

    ...

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

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>

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 ?

like image 592
MrDeveloper Avatar asked Jul 10 '15 09:07

MrDeveloper


1 Answers

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

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>

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>

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.

like image 165
MrDeveloper Avatar answered Oct 24 '22 02:10

MrDeveloper