log4Net config transform

Jack Smit picture Jack Smit · Oct 8, 2011 · Viewed 11k times · Source

I am able to transform my log4net.config but I am not getting this one. How do I change the following connectionString?

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <log4net debug="true">
        <root>
          <level value="ALL" />
        </root> 
        <logger name="MyApp">
          <level value="Debug" />
           <appender-ref ref="RollingLogFileAppender" />
           <appender-ref ref="AdoNetAppender" />
        </logger>
        <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
                <file value="C:\Websites\Logs\SiteLog.txt"/>
                <appendToFile value="true"/>
                <rollingStyle value="Date"/>
                <datePattern value="yyyyMMdd.txt"/>
                <layout type="log4net.Layout.PatternLayout">
                    <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline"/>
                </layout>
            </appender>
        <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
          <bufferSize value="1" />
          <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
          <connectionString value="***Data Source=(local);initial catalog=MyDB;User ID=sa;Password=dbPassword***" />

The following does not work:

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

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <log4net debug="false" 
           xdt:Transform="SetAttributes(debug)" />
  <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <connectionString value="Data Source=(local);initial catalog=RavenAxiom;User ID=ravenaxiom;Password=xyz" 
            xdt:Transform="SetAttributes(value)" />
  </appender>
</configuration>

It changes the debug value so it partially works. Thank for the help.

Jack

Answer

Jack Smit picture Jack Smit · Nov 30, 2011

Ok, got it working with: xdt:Locator="Match(name)" in appender name="AdoNetAppender" and then xdt:Transform="Replace" in connectionString value="NewConnValues".

<configuration>
  <log4net>
    <appender name="AdoNetAppender" 
              type="log4net.Appender.AdoNetAppender" 
              xdt:Locator="Match(name)">
      <connectionString value="YourConnectionStringToBe"  
                        xdt:Transform="Replace"/>
    </appender>
  </log4net>
</configuration>