查看日志以诊断管道问题

Azure DevOps Services |Azure DevOps Server |Azure DevOps Server 2022 |Azure DevOps Server 2020

管道日志提供了用于确定管道故障原因的强大工具,并且可以配置详细日志以提供更多诊断信息。

一个典型的起点是查看已完成的生成或发布中的日志。 可以通过导航到管道运行摘要并选择作业和任务来查看日志。 如果某个任务失败,请检查该任务的日志。 配置详细日志 以包含更多诊断信息。

配置详细日志

为了帮助进行故障排除,可以将日志配置为更详细。

  • 若要为单个运行配置详细日志,可以通过选择 “运行管道 ”并选择“ 启用系统诊断”和“ 运行”来启动新的生成。

    启用系统诊断

  • 若要为所有运行配置详细日志,可以添加一 system.debug 个名为并设置其值的 true变量。

Azure 管道日志现在可以捕获资源利用率指标,例如内存、CPU 使用率和可用磁盘空间。 日志还包括管道代理使用的资源和子进程(包括作业中运行的任务)。 如果怀疑管道作业可能遇到资源约束,请启用详细日志以将资源利用率信息注入管道日志。 资源利用率指标在任何代理上都可用,与托管模型无关。

若要查看捕获的资源利用率指标,请在日志中搜索Agent environment resources每个步骤的条目。

2024-02-28T17:41:15.1315148Z ##[debug]Agent environment resources - Disk: D:\ Available 12342.00 MB out of 14333.00 MB, Memory: Used 1907.00 MB out of 7167.00 MB, CPU: Usage 17.23%

查看和下载日志

若要查看每个步骤的单个日志,请导航到运行生成结果,然后选择作业和步骤。

任务日志

若要下载所有日志,请导航到运行生成结果,选择 ...,然后选择 “下载日志”。

下载日志

除了管道诊断日志之外,还可以使用以下专用日志类型,并可能包含有助于进行故障排除的信息。

辅助角色诊断日志

可以在生成代理上获取工作进程生成的已完成生成的诊断日志。 查找 worker 具有已完成生成日期和时间戳的日志文件。 例如,worker_20160623-192022-utc_6172.log

代理诊断日志

代理诊断日志提供代理的配置方式以及运行代理时发生的情况的记录。 查找 agent 日志文件。 例如,agent_20160624-144630-utc.log。 有两种类型的代理日志文件:

  • 运行 config.cmd时生成的日志文件。 此日志:

    • 在顶部附近包括以下行: Adding Command: configure

    • 显示所做的配置选择。

  • 运行 run.cmd时生成的日志文件。 此日志:

    • 在进程终止之前无法打开。

    • 尝试连接到 Azure DevOps 组织或 Team Foundation Server。

    • 显示每个作业的运行时间及其完成方式

这两个日志都显示如何检测和设置代理功能。

自托管代理的网络诊断

Agent.Diagnostic 的值设置为 true 以收集其他日志,这些日志可用于排查自托管代理的网络问题。

文件 信息 适用于
cloudinit.* Cloud-init 成功完成(如果使用) Linux
BrokenPackages.* 包处于一致状态 Linux
Agent.* 环境变量 Linux、Windows
waagentConf.txt Azure VM 代理 (waagent.conf) Azure:Linux、Windows
environment.txt / agent.* 帐户组成员身份列表 Windows操作系统

注释

Agent.Diagnostic 设置为 true 时,System.Debug 会自动设置为 true

本部分中介绍的 Agent.Diagnostic 变量和日志可用于 代理 v2.200.0 及更高版本。

有关详细信息,请参阅 microsoft/azure-pipelines-agent Azure Pipelines 代理开源代理存储库中的代理故障排除

其他日志

在诊断日志中,你将找到 environment.txtcapabilities.txt

该文件 environment.txt 包含有关运行生成的环境的各种信息。 这包括运行哪些任务、是否启用防火墙、PowerShell 版本信息和其他一些项等信息。 我们不断添加到此数据,使其更有用。

该文件 capabilities.txt 提供了一种干净的方式来查看运行生成的生成计算机上安装的所有功能。

HTTP 跟踪日志

重要

HTTP 跟踪和跟踪文件可以包含密码和其他机密。 不要在公共网站上发布它们。

使用内置的 HTTP 跟踪

如果代理版本为 2.114.0 或更高版本,则可以跟踪 HTTP 流量标头并将其写入诊断日志。 在 VSTS_AGENT_HTTPTRACE 启动 agent.listener 之前设置环境变量。

Windows:
    set VSTS_AGENT_HTTPTRACE=true

macOS/Linux:
    export VSTS_AGENT_HTTPTRACE=true

使用完整的 HTTP 跟踪 - Windows

  1. 启动 Fiddler

  2. 建议仅侦听代理流量。 文件 > 捕获流量关闭 (F12)

  3. 启用解密 HTTPS 流量。 工具 > Fiddler 选项 > HTTPS 选项卡。解密 HTTPS 流量

  4. 让代理知道使用代理:

    set VSTS_HTTP_PROXY=http://127.0.0.1:8888
    
  5. 以交互方式运行代理。 如果要作为服务运行,则可以在运行服务的帐户的控制面板中设置为环境变量。

  6. 重启代理。

使用完整的 HTTP 跟踪 - macOS 和 Linux

使用 Charles Proxy(类似于 Windows 上的 Fiddler)捕获代理的 HTTP 跟踪。

  1. 启动 Charles Proxy。

  2. Charles:代理 > 代理设置 > SSL 选项卡。启用。 添加 URL。

  3. 查尔斯:代理 > Mac OSX 代理。 建议禁用以仅查看代理流量。

    export VSTS_HTTP_PROXY=http://127.0.0.1:8888
    
  4. 以交互方式运行代理。 如果它作为服务运行,则可以在 .env 文件中设置。 请参见nix service

  5. 重启代理。

捕获自定义日志

除了内置日志之外,还可以使用任务和脚本在管道中捕获自定义日志。 以下示例演示如何捕获资源利用率、网络跟踪、内存转储和 perfview 跟踪。 如果使用的是客户支持,系统可能会要求你捕获日志,例如这些日志。

检索自定义日志

在管道中捕获自定义日志后,必须上传该日志,以便检索该日志以供审阅。 可以将自定义日志作为标准管道日志的一部分上传,也可以将其作为项目上传。 以下部分中的示例演示了上传自定义日志的两种方式。

将日志作为标准日志的一部分上传

若要将自定义日志作为标准管道日志的一部分上传,请使用 ##vso[task.uploadfile] 上传所需的文件。 若要使用此命令,请将其指定为脚本命令的一部分,如以下示例所示。 该文件可以 下载并作为 标准管道日志的一部分进行查看。 此方法 ##vso[task.uploadfile] 适用于上传单个日志文件。 如果有多个日志文件,则必须为每个文件使用单独的 ##vso[task.uploadfile] 行。

- pwsh: Write-Host "##vso[task.uploadfile]$(Agent.TempDirectory)\resource-usage.txt"

有关详细信息,请参阅 日志记录命令UploadFile:上传可使用任务日志下载的文件

将日志作为管道项目上传

若要将自定义日志作为管道项目上传,请使用 PublishPipelineArtifact@1 任务。 PublishPipelineArtifact@1 可以上传单个文件或目录路径中的文件,如果有许多要上传的自定义日志文件,则非常有用。

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(Pipeline.Workspace)/s/trace'
    artifact: 'file_result.pcap'
    publishLocation: 'pipeline'

有关详细信息,请参阅 发布管道项目

捕获资源利用率详细信息

使用 Azure DevOps Services 时,可以通过启用 详细日志来查看日志中的资源利用率,包括磁盘使用情况、内存使用情况和 CPU 利用率。 管道完成后,在日志中搜索Agent environment resources每个步骤的条目。

2024-02-28T17:41:15.1315148Z ##[debug]Agent environment resources - Disk: D:\ Available 12342.00 MB out of 14333.00 MB, Memory: Used 1907.00 MB out of 7167.00 MB, CPU: Usage 17.23%

如果使用 Azure DevOps Server,或者想要收集其他指标,可以使用 PowerShell 捕获资源利用率并将其上传到管道日志。 管道运行完成后,可以 下载管道日志并查看捕获的数据Upload resource usage from pipeline run如果该步骤是作业中的第六个步骤,则日志中的文件名将 6_resource-usage.txt

# Place this task in your pipeline to log the current resource utilization
# of the pipeline. This task appends the specified resource usage to a logfile
# which is uploaded at the end of the current pipeline job.
- pwsh: |
      $logFile = '$(Agent.TempDirectory)\resource-usage.txt'
      if (!(Test-Path $logFile))
      {
        New-Item $logFile
      }
      Get-Date | Out-File -FilePath $logFile -Append
      Get-Volume | Out-File -FilePath $logFile -Append
      Get-Counter '\Memory\Available MBytes' | Out-File -FilePath $logFile -Append
      Get-Counter '\Processor(_Total)\% Processor Time' | Out-File -FilePath $logFile -Append
      sleep 10
  displayName: 'Check resource utilization'

# Other tasks here, and you can repeat the "Check resource utilization"
# step if desired, and the results will be appended to the resource-usage.txt file

- pwsh: Write-Host "##vso[task.uploadfile]$(Agent.TempDirectory)\resource-usage.txt"
  displayName: 'Upload resource usage from pipeline run'
  condition: always()

使用 ProcDump 捕获 dotnet 进程内存转储

如果测试执行崩溃,客户支持可能会要求你在测试执行失败后捕获 dotnet 进程的内存转储。 使用 Visual Studio 测试任务后添加以下任务 condition: always()。 管道运行完成后,可以 下载管道日志,包括内存转储

# Run this task after your test execution crashes
# with a condition of alway() so that it always runs
- pwsh: |
    Invoke-WebRequest https://download.sysinternals.com/files/Procdump.zip -OutFile $(Agent.TempDirectory)\Procdump.zip
    mkdir $(Agent.TempDirectory)\Procdump
    unzip $(Agent.TempDirectory)\Procdump.zip -d Procdump
    cd $(Agent.TempDirectory)\Procdump
    Get-Process dotnet | % { $(Agent.TempDirectory)\procdump.exe -accepteula -ma $_.Id dotnet-$($_.Id).dmp }
    Compress-Archive *.dmp -DestinationPath $(Agent.TempDirectory)\dump_files.zip
    Write-Host "##vso[task.uploadfile]$(Agent.TempDirectory)\dump_files.zip"
  condition: always()
  displayName: 'Create and upload a dotnet process memory dump'

捕获托管代理的 ETW 跟踪

如果要排查Microsoft托管代理的网络问题,客户支持可能会要求你收集 ETW 跟踪。 管道运行完成后,可以 下载管道日志,包括 ETW 跟踪

# Add this task to start the ETW trace
- script: netsh trace start scenario=InternetClient capture=yes tracefile=$(Agent.TempDirectory)\networktrace.etl
  displayName: 'Start ETW trace'

# Other tasks here

# Add these 2 tasks to stop the trace and upload
# the trace to the pipeline logs
- script: netsh trace stop
  displayName: 'Stop ETW trace'

- pwsh: |
    Write-Host "##vso[task.uploadfile]$(Agent.TempDirectory)\networktrace.etl"
    Write-Host "##vso[task.uploadfile]$(Agent.TempDirectory)\networktrace.cab"
  displayName: 'Upload ETW trace logs'

捕获 perfview Visual Studio 生成的跟踪

如果客户支持要求你创建 perfview Visual Studio 生成的跟踪,请在 Visual Studio 生成步骤前后将以下任务添加到管道。

运行管道后,可以从管道运行详细信息下载 PerfViewLog 项目,并发送该文件客户支持。

steps:
- task: PowerShell@2 # download the perfview exe
  inputs:
    targetType: 'inline'
    script: |
      invoke-webrequest https://github.com/microsoft/perfview/releases/download/v3.1.7/PerfView.exe -OutFile PerfView.exe

- task: PowerShell@2
  inputs:
    targetType: 'inline' # start perfview to capture the traces before build build task
    script: '$(System.DefaultWorkingDirectory)\PerfView.exe "/DataFile:PerfViewData.etl" /accepteula /BufferSizeMB:512 /StackCompression /CircularMB:5000 /Providers:"Microsoft-Windows-IIS" /logfile:"PerfView.log" /zip:true /norundown start'

- task: VSBuild@1
  displayName: '$(solution)' # build of the solution, note the msbuildargs might be different for your scenario
  inputs:
    solution: '$(solution)'
    clean: true
    msbuildArgs: '/p:DeployOnBuild=true /p:PrecompileBeforePublish=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(Build.ArtifactStagingDirectory)" /p:TransformWebConfigEnabled=false /p:AutoParameterizationWebConfigConnectionStrings=false /p:MarkWebConfigAssistFilesAsExclude=false /p:ProfileTransformWebConfigEnabled=false /p:IsTransformWebConfigDisabled=true'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: PowerShell@2 # stop the perfview tracing
  inputs:
    targetType: 'inline' 
    script: |
      $(System.DefaultWorkingDirectory)\perfview.exe /accepteula /logfile:"PerfView.log" stop

- task: PowerShell@2 # abort perfview, it seems required.
  inputs:
    targetType: 'inline'
    script: '$(System.DefaultWorkingDirectory)\perfview.exe /accepteula /logfile:"PerfView.log" abort'

- task: PowerShell@2 # add a sleep of 5 mins, to give it time for required traces to be complete
  inputs:
    targetType: 'inline'
    script: 'Start-Sleep -Seconds 300'

- task: PublishPipelineArtifact@1 # upload the traces
  displayName: 'Publish Pipeline Artifact'
  inputs:
    artifactName: webapp