Dotnet框架-在TFS CD管道中使用变量替换进行Nlog配置更改

问题描述

我在Web配置文件中具有Nlog配置,并且我想更改CD管道中的文件路径,以便根据环境放置一些动态路径。

当前,web.config文件变量替换(“ XML变量替换”选项)不支持它。

还有哪些其他方法可以完成此操作?我真的没有选择去使用Web.Config转换方法

任何有关此的指导都将真正有帮助。

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  autoReload="true"
  throwExceptions="false"
  internalLogLevel="Error" internalLogFile="c:\Logs\nlog-internal.log">
    <targets name="nlogconfig" async="true">
      <target xsi:type="File" name="name"
         fileName="Path/${shortdate}.log"
         archiveFileName="Path/${shortdate}.{###}.log"
         layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true:skipFrames=1:cleanNamesOfAnonymousDelegates=true} ${newline} ${message} ${newline} ${exception:innerFormat=ToString:maxInnerExceptionLevel=2:innerExceptionSeparator=newline:separator=newline:format=ToString,StackTrace}${newline}"
         archiveAboveSize="8388608"
         archiveNumbering="Rolling"
         archiveEvery="Day"
         concurrentWrites="true"
         maxArchiveFiles="100" />
    </targets>
    <rules>
      <logger name="*" minlevel="Debug" writeto="name" />
    </rules>
  </nlog>

解决方法

还有哪些其他方法可以完成?

您可以使用Replace Tokens Extension中的替换令牌任务

这是我的步骤,您可以参考它们:

Nlog配置:

<targets>
    <target name="logfile" xsi:type="File" fileName="#{variable}#/#{shortdate}#.log />
    <target name="logconsole" xsi:type="Console" />
</targets>

替换令牌任务示例:

- task: replacetokens@3
  inputs:
    rootDirectory: 'Folder Path'
    targetFiles: '**/*.config'
    encoding: 'auto'
    writeBOM: true
    actionOnMissing: 'warn'
    keepToken: false
    tokenPrefix: '#{'
    tokenSuffix: '}#'
    useLegacyPattern: false
    enableTelemetry: true

变量:

enter image description here

然后Nlog配置中的变量将被替换。

enter image description here

,

另一种解决方案是在默认plt.subplots()旁边部署特定于环境的替代文件。

特定于环境的NLog.config的示例:

NLog.override.config

<nlog> <variable name="LogDirectory" value="D:/Path" /> </nlog> 的示例:

NLog.config

部署程序包可能包含多个 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <variable name="LogDirectory" value="${basedir}" /> <!-- Default Value --> <include file="NLog.override.config" ignoreErrors="true" /> <!-- Override Value --> <targets async="true"> <target xsi:type="File" name="name" fileName="${LogDirectory}/${shortdate}.log" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="name" /> </rules> </nlog> 文件。每个环境一个,然后根据所选环境部署正确的一个。

另请参阅:https://github.com/nlog/nlog/wiki/Configuration-file#include-files