Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

web.config xpath transform - find by parent element

I am trying to change a log file name after deploying, so transform this:

<log4net>
    ...
    <appender name="GeneralAppender" type="log4net.Appender.RollingFileAppender, log4net">
        <file value="c:\logs\Co.App.log" />
    ...
    </appender>
</log4net>

to this:

<log4net>
    ...
    <appender name="GeneralAppender" type="log4net.Appender.RollingFileAppender, log4net">
        <file value="c:\logs\Co.App.localhost.log" />
    ...
    </appender>
</log4net>

the actual file node doesn't have any attributes, so I am trying to locate it by parent node

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <log4net>
        <appender >
          <file value="c:\logs\Co.App.localhost.log" xdt:Transform="Replace" xdt:Locator="XPath(../appender[@name='GeneralAppender'])" />
        </appender>
      </log4net>
    </assemblyBinding>
  </runtime>

i've also tried all permutations of absolute and relative xpath's but i don't see it having any effect in transform preview.

i tried:

xdt:Locator="XPath(//appender[@name='GeneralAppender']/file)"

and even:

xdt:Transform="Remove" xdt:Locator="XPath(//file)"

like image 263
Sonic Soul Avatar asked Mar 14 '14 23:03

Sonic Soul


2 Answers

found it!

<file value="c:\logs\Co.App.local.log" xdt:Transform="Replace" xdt:Locator="Condition(../@name='GeneralAppender')" />
like image 76
Sonic Soul Avatar answered Nov 20 '22 21:11

Sonic Soul


Slight extension:

If the parent node has TWO (or more children), then the above solution is not enough.

This is the case in log4net, when the EventLogAppend is used, which has:

  <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" >
    ...
    <param name="LogName" value="MyLog"/>
    <param name="ApplicationName" value="MyApplication"/>
    ...

Then you need to use an 'and' + an atribute more to hit the right node:, like xdt:Locator="Condition(../@name='EventLogAppender' and @name='LogName')

Example for the EventLogAppender, where both the params are replaced:

<param name="LogName" value="MyNewLog" xdt:Transform="Replace" xdt:Locator="Condition(../@name='EventLogAppender' and @name='LogName')" />
<param name="ApplicationName" value="MyNewApplication" xdt:Transform="Replace" xdt:Locator="Condition(../@name='EventLogAppender' and @name='ApplicationName')" />
like image 3
Toke Breer-Mortensen Avatar answered Nov 20 '22 21:11

Toke Breer-Mortensen