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...
ConnectionStringsin theweb.configusing${appsetting:item=connectionStrings.Database}. Maybe it is easier to find examples of how to transform theConnectionStrings-section in app.config. See also: github.com/NLog/NLog/wiki/AppSetting-Layout-Renderer