Search code examples
asp.netweb-config-transformxdt-transform

Using Web.Config Transforms to Change the "size" element in a log4net "parameter"


I'm working on an ASP.NET project which uses log4net. In the Development environment, I want the size element of the @stackTrace parameter to be set to a higher value than in other environments.

The structure of the log4net.config file is:

<?xml version="1.0"?>
<configuration>
    <log4net debug="true">
        <appender name="SQLServerAppender" type="log4net.Appender.AdoNetAppender">
            <parameter>
                <parameterName value="@stackTrace"/>
                <dbType value="String"/>
                <size value="1000"/>
                <layout type="log4net.Layout.RawPropertyLayout">
                    <key value="stackTrace"/>
                </layout>
            </parameter>
            <!-- More parameters -->
        </appender>
    </log4net>
</configuration>

I would like to change the value attribute of the size element to 2000.

I tried the following transform file, but it didn't change anything:

  <?xml version="1.0" encoding="utf-8"?>
  <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <log4net>
      <appender name="SQLServerAppender" type="log4net.Appender.AdoNetAppender">
        <parameter xdt:Locator="XPath(configuration/log4net/appender[@name='SQLServerAppender']/parameter[parameterName[@value='@stackTrace']])"
                   xdt:Transform="Remove">
        </parameter>
      </appender>
    </log4net>
  </configuration>

The "Remove" was a last resort to try to get something to happen!

What should I do to perform the desired transform? It's not clear to me how to combine xdt:Locator with xdt:Transform in this case.


Solution

  • After the answer by Eric.Y.Fan didn't work, I played around a bit to find out why not.

    I first put back the <connectionString> value (I left it out of my post for clarity), and it did work. That proved that the correct <appender> had been found, but that the correct <parameter> was not being found. "Found", or "located". That was a hint.

    I looked at the XPath expression, and realized that it was attempting to locate the <parameter> which had a <parameterName> with a value attribute with the value @stackTrace. So I tried using Condition:

    <parameter xdt:Locator="Condition([parameterName[@value='@stackTrace']])" 
        xdt:Transform="Replace">
    </parameter>
    

    This worked!

    So the final transform is:

    <parameter xdt:Locator="Condition([parameterName[@value='@stackTrace']])">
        <size value="2000" xdt:Transform="Replace" />
    </parameter>