Question

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.

Was it helpful?

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>

OTHER TIPS

I could be mistaken but I don't think Web.config transforms can be applied to other arbitrary xml files.

For that purpose I usually use SlowCheetah: http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5

It's a great tool, very easy to use (similar to web.config transforms, but can be applied to anything), and also integrates very well with automated builds and deployments.

Here's a guide from Scott Hanselman: http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx

Try this:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<log4net>
  <appender name="SQLServerAppender" type="log4net.Appender.AdoNetAppender">
    <parameter>
       <size value="2000" xdt:Locator="XPath(configuration/log4net/appender[@name='SQLServerAppender']/parameter[parameterName[@value='@stackTrace']])" xdt:Transform="SetAttributes"/>
    </parameter>
  </appender>
</log4net>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top