Warm tip: This article is reproduced from serverfault.com, please click

How to access log4net appender file value and replace in the azure pipelines

发布于 2020-11-30 16:25:06

I am trying to replace log file path from pipeline

<configuration>
 <configSections>
  <applicationSettings>
   <log4net debug="true">
    <appender name="appender1">
        <file value="log.txt"/>
    </appender>
   </log4net>
  </applicationSettings>
 </configSections>
</configuration>

I am using FileTransform task to achieve this. How to access log4net appender file value and replace in the azure pipelines What and How should be the "Name" to be set in the variable. I want the "Value" to be set as "D:\Logs\log.txt". Thanks.

Questioner
Programmer
Viewed
0
Levi Lu-MSFT 2020-12-01 11:30:34

As it is described in the FileTransform task document. The variables defined in pipeline will be matched against the key or name entries in the appSettings, applicationSettings, and connectionStrings sections.

Variables defined in the build or release pipelines will be matched against the 'key' or 'name' entries in the appSettings, applicationSettings, and connectionStrings sections of any config file and parameters.xml. Variable Substitution is run after config transforms.

So you can add a name attribute to the file property in your config file. For example in below. I add a name attribute LogFile:

 <applicationSettings>
   <log4net debug="true">
    <appender name="appender1">
        <file name="LogFile" value="log.txt"/>
    </appender>
   </log4net>
  </applicationSettings>

Then you can define a variabled named LogFile in Variables section of your azure devops pipeline. See below example:

variables:
  LogFile: D:\Logs\log.txt

steps:
- task: FileTransform@1
  inputs:
    folderPath: $(System.DefaultWorkingDirectory)
    fileType: xml
    

If you donot want to add a name attribute to file property. You can use Task Magic Chuncks to replace the value of file property.

Set the transformations: configuration/configSections/applicationSettings/log4net/appender/file/@value": "D:\Logs\log.txt". See below example:

See here about how to transform XML files

steps:
- task: sergeyzwezdin.magic-chunks.magic.chunks.MagicChunks@2
  displayName: 'Config transform'
  inputs:
    sourcePath: '$(System.DefaultWorkingDirectory)/path/to/app.config'
    transformations: |
     {
       "configuration/configSections/applicationSettings/log4net/appender/file/@value": "D:\Logs\log.txt"
      }