WebDeploy, parameters.xml transforms and nLog settings
I have been trying to parameterise the SQL DB connection string used by nLog when it is defined in a web.config file of a web site being deployed via Release Management and WebDeploy i.e. I wanted to select and edit the bit highlighted of my web.config file
1<configuration>
2 <nlog xmlns="[http://www.nlog-project.org/schemas/NLog.xsd"](http://www.nlog-project.org/schemas/NLog.xsd") xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance")\>
3
4 <targets async="true">
5 <target xsi:type="Database" name="SQL" dbProvider="System.Data.SqlClient" connectionString="Data Source=myserver;Database=mydb;Persist Security Info=True;Pooling=False" keepConnection="true" commandText="INSERT INTO \[Logs\](ID, TimeStamp, Message, Level, Logger, Details, Application, MachineName, Username) VALUES(newid(), getdate(), @message, @level, @logger, @exception, @application, @machineName, @username)">
6
7 <parameter layout="${message}" name="@message"></parameter>
8 …….
9
The problem I had was that the xpath query I was using was not returning the nLog node because the nLog node has a namespace defined. This means we can’t just use a query in the form
1<parameter name="NLogConnectionString" description="Description for NLogConnectionString" defaultvalue="\_\_NLogConnectionString\_\_" tags="">
2 <parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/nlog/targets/target\[@name='SQL'\]/@connectionString" />
3</parameter>
I needed to use
1<parameter name="NLogConnectionString" description="Description for NLogConnectionString" defaultvalue="\_\_NLogConnectionString\_\_" tags="">
2 <parameterentry kind="XmlFile" scope="\\web.config$" match="/configuration/\*\[local-name() = 'nlog'\]/\*\[local-name() = 'targets'\]/\*\[local-name() = 'target' and @name='SQL'\]/@connectionString" />
3</parameter>
So more complex, but it does work. Hopefully this will save others the time I wasted working it out today