0

I would like to transform a part of my 'web.config' file using a transformation file, but the value I am trying to change is within sections with attributes and the transformation does not occur if I do not specify the attributes of every previous section.

Here is the original 'web.config' section :

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="true"
      internalLogLevel="Trace" internalLogFile="c:\projets\nlog-internal.log">
    <targets>
      <target name="database" xsi:type="Database" dbProvider="Oracle.ManagedDataAccess.Client.OracleConnection, Oracle.ManagedDataAccess" keepConnection="false" useTransactions="true"
            connectionString="Data Source=myInstance;User Id=myUserId;Password=myPassword;Validate Connection=true"
            commandType="Text" commandText="INSERT INTO LOG (DATE_LOG, LEVEL_LOG, MESSAGE, LOGGER, EXCEPTION) VALUES (TO_DATE(SUBSTR(:LogDate, 1, 19), 'YYYY/MM/DD HH24:MI:SS'), :LogLevel, :Message, :Logger, :Exception)">
        <parameter name="LogDate" layout="${date:format=yyyy/MM/dd HH\:mm\:ss}" />
        <parameter name="LogLevel" layout="${level}" />
        <parameter name="Message" layout="${message}" />
        <parameter name="Logger" layout="${logger}" />
        <parameter name="Exception" layout="${exception:tostring}" />
      </target>
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="database" />
    </rules>
  </nlog>

I would like to change the value of the 'connectionString' attribute of the 'target' with the name 'database'.

Here is the transformation that worked fine :

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
    <targets>
      <target name="database" connectionString="Data Source=myNewInstance;User Id=myNewUser;Password=myNewPassword;Validate Connection=true" xdt:Locator="Match(name)" xdt:Transform="SetAttributes(connectionString)"></target>
    </targets>
  </nlog>

My question is :

Is there a way to tell the transformation file to always alter the first 'nlog' section of the 'web.config' instead of having to write all the xmlns and xsi attributes ? It would be cleaner to only have something like this :

<nlog>
    <targets>
      <target name="database" connectionString="Data Source=myNewInstance;User Id=myNewUser;Password=myNewPassword;Validate Connection=true" xdt:Locator="Match(name)" xdt:Transform="SetAttributes(connectionString)"></target>
    </targets>
  </nlog>

But it does not work, unfortunately...

1
  • You can reference ConnectionStrings in the web.config using ${appsetting:item=connectionStrings.Database}. Maybe it is easier to find examples of how to transform the ConnectionStrings-section in app.config. See also: github.com/NLog/NLog/wiki/AppSetting-Layout-Renderer Commented Aug 19, 2024 at 16:12

1 Answer 1

0

I use SlowCheetah to transform all my config files. Works for anything *.config related. Add Build Configuration transformations for different deploys with ease.

Not sure if that is an option for you.

Microsoft SlowCheetah

Noticed in your title that you use Web.config, Web.Config in a Web App will only transform on a publish not build config complie just so you know.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.