查看和管理任务运行日志View and manage task run logs

Azure 容器注册表任务中的每个任务运行都会生成日志输出,检查该输出即可确定任务步骤是否已成功运行。Each task run in Azure Container Registry tasks generates log output that you can inspect to determine whether the task steps ran successfully.

本文介绍了如何查看和管理任务运行日志。This article explains how to view and manage task run logs.

查看流式传输的日志View streamed logs

手动触发任务时,日志输出会直接流式传输到控制台。When you trigger a task manually, log output is streamed directly to the console. 例如,使用 az acr buildaz acr runaz acr task run 命令手动触发任务时,你会看到流式传输到控制台的日志输出。For example, when you trigger a task manually by using the az acr build, az acr run, or az acr task run command, you see log output streamed to the console.

以下示例 az acr run 命令手动触发一个任务,该任务运行从同一注册表中拉取的容器:The following sample az acr run command manually triggers a task that runs a container pulled from the same registry:

az acr run --registry mycontainerregistry1220 \
  --cmd '$Registry/samples/hello-world:v1' /dev/null

流式传输的日志:Streamed log:

Queued a run with ID: cf4
Waiting for an agent...
2020/03/09 20:30:10 Alias support enabled for version >= 1.1.0, please see https://aka.ms/acr/tasks/task-aliases for more information.
2020/03/09 20:30:10 Creating Docker network: acb_default_network, driver: 'bridge'
2020/03/09 20:30:10 Successfully set up Docker network: acb_default_network
2020/03/09 20:30:10 Setting up Docker configuration...
2020/03/09 20:30:11 Successfully set up Docker configuration
2020/03/09 20:30:11 Logging in to registry: mycontainerregistry1220.azurecr.cn
2020/03/09 20:30:12 Successfully logged into mycontainerregistry1220.azurecr.cn
2020/03/09 20:30:12 Executing step ID: acb_step_0. Timeout(sec): 600, Working directory: '', Network: 'acb_default_network'
2020/03/09 20:30:12 Launching container with name: acb_step_0
Unable to find image 'mycontainerregistry1220.azurecr.cn/samples/hello-world:v1' locally
v1: Pulling from samples/hello-world
Digest: sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e888a
Status: Downloaded newer image for mycontainerregistry1220.azurecr.cn/samples/hello-world:v1

Hello from Docker!
This message shows that your installation appears to be working correctly.
[...]

2020/03/09 20:30:13 Successfully executed container: acb_step_0
2020/03/09 20:30:13 Step ID: acb_step_0 marked as successful (elapsed time in seconds: 1.180081)

Run ID: cf4 was successful after 5s

查看存储的日志View stored logs

Azure 容器注册表存储所有任务的运行日志。Azure Container Registry stores run logs for all tasks. 可以在 Azure 门户中查看已存储的运行日志。You can view stored run logs in the Azure portal. 也可使用 az acr task logs 命令查看选定的日志。Or, use the az acr task logs command to view a selected log. 默认情况下,日志保留 30 天。By default, logs are retained for 30 days.

如果任务是自动触发的(例如,由源代码更新触发),则访问已存储的日志是查看运行日志的唯一方法。 If a task is automatically triggered, for example by a source code update, accessing the stored logs is the only way to view the run logs. 任务自动触发器包括源代码提交或拉取请求、基础映像更新和计时器触发器。Automatic task triggers include source code commits or pull requests, base image updates, and timer triggers.

若要在门户中查看运行日志,请执行以下操作:To view run logs in the portal:

  1. 导航到你的容器注册表。Navigate to your container registry.
  2. 在“服务” 中,选择 “任务” > “运行” 。In Services, select Tasks > Runs.
  3. 选择一个“运行 ID” 来查看运行状态和运行日志。Select a Run Id to view the run status and run logs. 运行日志包含与流式传输的日志(如果生成了该日志)相同的信息。The log contains the same information as a streamed log, if one is generated.

查看任务运行登录门户

若要使用 Azure CLI 查看日志,请运行 az acr task logs 并指定运行 ID、任务名称,或指定由生成任务创建的特定映像。To view a log using the Azure CLI, run az acr task logs and specify a run ID, a task name, or a specific image create by a build task. 如果指定了任务名称,则该命令会显示上次创建的运行的日志。If a task name is specified, the command shows the log for the last created run.

以下示例输出 ID 为 cf4 的运行的日志:The following example outputs the log for the run with ID cf4:

az acr task logs --registry mycontainerregistry1220 \
  --run-id cf4

替代日志存储Alternative log storage

你可能希望将任务运行日志存储在本地文件系统上,或使用替代存档解决方案,例如 Azure 存储。You might want to store task run logs on a local file system or use an alternative archiving solution such as Azure Storage.

例如,创建一个本地 tasklogs 目录,并将 az acr task logs 的输出重定向到一个本地文件:For example, create a local tasklogs directory, and redirect the output of az acr task logs to a local file:

mkdir ~/tasklogs

az acr task logs --registry mycontainerregistry1220 \
  --run-id cf4 > ~/tasklogs/cf4.log

还可以将本地日志文件保存到 Azure 存储。You can also save local log files to Azure Storage. 例如,使用 Azure CLIAzure 门户或其他方法将文件上传到某个存储帐户。For example, use the Azure CLI, the Azure portal, or other methods to upload files to a storage account.

后续步骤Next steps