Change appender param value in .config files on release ( azure pipelines)

All we need is an easy explanation of the problem, so here it is.

I am using log4net for logging on almost all projects in my .net solution.
Currently, I have configured an appender (AzureTableAppender), configuration that is made in each project’s respective app.config/ web.config.
This appender has a param named “TableName”, with an associated value that represents the table to which the data is sent. Everything works fine, but I would like to change that value for 2 of my releases as I want the logs somewhere else.
The config looks like this:

<log4net>
      <appender type="log4net.Appender.AzureTableAppender.......
          <param name = "TableName" value = "MyTable" />
....
</log4net>

To sum up, I would want to change the “TableName” value for all .config files(app, web), depending on release.
I am using Azure Pipelines for my releases . ( as I use pipeline variables from gui I thought of that, but I think those target only appSettings, connectionStrings)

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can try Replace Token task.

1.Change your content of <log4net> element to this format:

<log4net>
      <appender type="log4net.Appender.AzureTableAppender.......
            <param name = "TableName" value = "#{MyTable}#" />
            <param name = "ReleaseName" value = "#{MyRelease}#" />
</log4net>

2.Create two variable groups(one for DEV and another for Release) with different content. In your release pipeline link them and choose corresponding scope:

enter image description here

When the Replace Token task executes, it will replace the #{MyTable}# and #{MyRelease}# with corresponding value.(Note this task won’t work in files inside xx.zip file) Hope it helps and if I misunderstand your requirements, feel free to correct me.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply