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

问题描述 投票:0回答:2

我在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>
tfs nlog variable-substitution
2个回答
2
投票

替代解决方案是在默认值旁边部署特定于环境的覆盖文件

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>

部署包可以包含多个

nlog.override.config
文件。每个环境一个,只需根据所选环境部署正确的一个即可。

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

另请参阅:https://github.com/NLog/NLog/wiki/Environment-specific-NLog-Logging-Configuration


2
投票

还有哪些其他方法可以做到这一点?

您可以使用替换令牌扩展中的替换令牌任务

这是我的步骤,你可以参考一下:

Nlog配置:

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

替换Token任务示例:

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

变量:

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

© www.soinside.com 2019 - 2024. All rights reserved.