AzureLogCollector 扩展AzureLogCollector Extension

重要

此扩展已被弃用,列在此处仅供参考。This extension has been deprecated and is only documented here for informational purposes only. 不建议在部署中继续使用此扩展。We do not recommend you continue to use this extension within your deployments.

若要诊断 Azure 云服务的问题,需要在问题发生时收集虚拟机上该服务的日志文件。Diagnosing issues with a Azure cloud service requires collecting the service's log files on virtual machines as the issues occur. 可以使用 AzureLogCollector 扩展按需从一个或多个云服务 VM(通过 Web 角色和辅助角色)执行一次性日志收集,并将收集到的文件传输到 Azure 存储帐户 - 所有这些操作都无需远程登录到任何 VM。You can use the AzureLogCollector extension on-demand to perform one-time collection of logs from one or more Cloud Service VMs (from both web roles and worker roles) and transfer the collected files to an Azure storage account - all without remotely logging on to any of the VMs.

备注

有关大多数记录的信息的说明,请参阅 http://blogs.msdn.com/b/kwill/archive/2013/08/09/windows-azure-paas-compute-diagnostics-data.aspDescriptions for most of the logged information can be found at http://blogs.msdn.com/b/kwill/archive/2013/08/09/windows-azure-paas-compute-diagnostics-data.asp.

有两种收集模式,其使用取决于要收集的文件的类型。There are two modes of collection dependent on the types of files to be collected.

  • 仅 Azure 来宾代理日志 (GA)Azure Guest Agent Logs only (GA). 此收集模式包括与 Azure 来宾代理以及其他 Azure 组件相关的所有日志。This collection mode includes all the logs related to Azure guest agents and other Azure components.

  • 所有日志(完整)All Logs (Full). 此收集模式会收集 GA 模式下的所有文件以及:This collection mode collects all files in GA mode plus:

    • 系统和应用程序事件日志system and application event logs
    • HTTP 错误日志HTTP error logs
    • IIS 日志IIS Logs
    • 安装程序日志Setup logs
    • 其他系统日志other system logs

在两种收集模式下,均可使用以下结构的集合来指定额外的数据收集文件夹:In both collection modes, additional data collection folders can be specified by using a collection of the following structure:

  • Name:集合的名称,用作已收集文件所在的 zip 文件中子文件夹的名称。Name: The name of the collection, used as the name of the subfolder inside the zip file with the collected files.
  • Location:要收集文件所在虚拟机上的文件夹路径。Location: The path to the folder on the virtual machine where files to be collected are located.
  • SearchPattern:要收集的文件名的样式。SearchPattern: The pattern of the names of files to be collected. 默认值为“*”Default is "*"
  • Recursive:如果要收集的文件以递归方式列于指定位置下。Recursive: if the files to be collected are located recursively under the location specified.

必备条件Prerequisites

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

  • 有一个用于扩展的存储帐户,保存生成的 zip 文件。Have a storage account for extension to save generated zip files.
  • Azure PowerShell。Azure PowerShell. 有关安装说明,请参阅安装 Azure PowerShellSee Install Azure PowerShell] for install instructions.

添加扩展Add the extension

可以使用 Azure PowerShell cmdlet 或服务管理 REST API 添加 AzureLogCollector 扩展。You can use Azure PowerShell cmdlets or Service Management REST APIs to add the AzureLogCollector extension.

对于云服务,可以使用现有的 Azure Powershell cmdlet Set-AzureServiceExtension 启用云服务角色实例上的扩展。For Cloud Services, the existing Azure Powershell cmdlet, Set-AzureServiceExtension, can be used to enable the extension on Cloud Service role instances. 每次通过此 cmdlet 启用此扩展时,都会在所选角色的所选角色实例上触发日志收集。Every time this extension is enabled through this cmdlet, log collection is triggered on the selected role instances of selected roles.

对于虚拟机,可以使用现有的 Azure Powershell cmdlet Set-AzureVMExtension 启用虚拟机上的扩展。For Virtual Machines, the existing Azure Powershell cmdlet, Set-AzureVMExtension, can be used to enable the extension on Virtual Machines. 每次通过 cmdlet 启用此扩展时,都会在每个实例上触发日志收集。Every time this extension is enabled through the cmdlets, log collection is triggered on each instance.

在内部,此扩展使用基于 JSON 的 PublicConfiguration 和 PrivateConfiguration。Internally, this extension uses the JSON-based PublicConfiguration and PrivateConfiguration. 下面是公共和私有配置的示例 JSON 的布局。The following is the layout of a sample JSON for public and private configuration.

PublicConfigurationPublicConfiguration

{
    "Instances":  "*",
    "Mode":  "Full",
    "SasUri":  "SasUri to your storage account with sp=wl",
    "AdditionalData":
    [
      {
              "Name":  "StorageData",
              "Location":  "%roleroot%storage",
              "SearchPattern":  "*.*",
              "Recursive":  "true"
      },
      {
            "Name":  "CustomDataFolder2",
            "Location":  "c:\customFolder",
            "SearchPattern":  "*.log",
            "Recursive":  "false"
      },
    ]
}

PrivateConfigurationPrivateConfiguration

{

}

备注

此扩展不需要 privateConfigurationThis extension doesn't need privateConfiguration. 可以只为 –PrivateConfiguration 参数提供一个空的结构。You can just provide an empty structure for the -PrivateConfiguration argument.

可以遵循以下两个步骤之一,将 AzureLogCollector 添加到所选角色的云服务或虚拟机的一个或多个实例,以便在每个 VM 上触发收集操作,从而运行收集的文件并将其发送给指定的 Azure 帐户。You can follow one of the two following steps to add the AzureLogCollector to one or more instances of a Cloud Service or Virtual Machine of selected roles, which triggers the collections on each VM to run and send the collected files to Azure account specified.

作为服务扩展添加Adding as a Service Extension

  1. 按照说明将 Azure PowerShell 连接到订阅。Follow the instructions to connect Azure PowerShell to your subscription.

  2. 指定服务名、槽、角色和角色实例,以便向其添加 AzureLogCollector 扩展并启用该扩展。Specify the service name, slot, roles, and role instances to which you want to add and enable the AzureLogCollector extension.

    #Specify your cloud service name
    $ServiceName = 'extensiontest2'
    
    #Specify the slot. 'Production' or 'Staging'
    $slot = 'Production'
    
    #Specified the roles on which the extension will be installed and enabled
    $roles = @("WorkerRole1","WebRole1")
    
    #Specify the instances on which extension will be installed and enabled.  Use wildcard * for all instances
    $instances = @("*")
    
    #Specify the collection mode, "Full" or "GA"
    $mode = "GA"
    
  3. 指定更多需要为其收集文件的数据文件夹(此步骤为可选)。Specify the additional data folder for which files will be collected (this step is optional).

    #add one location
    $a1 = New-Object PSObject
    
    $a1 | Add-Member -MemberType NoteProperty -Name "Name" -Value "StorageData"
    $a1 | Add-Member -MemberType NoteProperty -Name "SearchPattern" -Value "*"
    $a1 | Add-Member -MemberType NoteProperty -Name "Location" -Value "%roleroot%storage"  #%roleroot% is normally E: or F: drive
    $a1 | Add-Member -MemberType NoteProperty -Name "Recursive" -Value "true"
    
    $AdditionalDataList+= $a1
    #more locations can be added....
    

    备注

    可以使用令牌 %roleroot% 指定角色根驱动器,因为该角色不使用固定驱动器。You can use token %roleroot% to specify the role root drive since it doesn't use a fixed drive.

  4. 提供要向其上传所收集文件的 Azure 存储帐户名称和密钥。Provide the Azure storage account name and key to which collected files will be uploaded.

    $StorageAccountName = 'YourStorageAccountName'
    $StorageAccountKey  = 'YourStorageAccountKey'
    
  5. 按如下所示调用 SetAzureServiceLogCollector.ps1(本文末尾提供),以便为云服务启用 AzureLogCollector 扩展。Call the SetAzureServiceLogCollector.ps1 (included at the end of the article) as follows to enable the AzureLogCollector extension for a Cloud Service. 执行完以后,可以在 https://YourStorageAccountName.blob.core.chinacloudapi.cn/vmlogs 下找到上传的文件Once the execution is completed, you can find the uploaded file under https://YourStorageAccountName.blob.core.chinacloudapi.cn/vmlogs

    .\SetAzureServiceLogCollector.ps1 -ServiceName YourCloudServiceName  -Roles $roles  -Instances $instances -Mode $mode -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey -AdditionDataLocationList $AdditionalDataList
    

下面是传递给脚本的参数的定义。The following is the definition of the parameters passed to the script. (也在下面复制。)(This is copied below as well.)

[CmdletBinding(SupportsShouldProcess = $true)]

param (
  [Parameter(Mandatory=$true)]
  [string]   $ServiceName,

  [Parameter(Mandatory=$false)]
  [string[]] $Roles ,

  [Parameter(Mandatory=$false)]
  [string[]] $Instances,

  [Parameter(Mandatory=$false)]
  [string]   $Slot = 'Production',

  [Parameter(Mandatory=$false)]
  [string]   $Mode = 'Full',

  [Parameter(Mandatory=$false)]
  [string]   $StorageAccountName,

  [Parameter(Mandatory=$false)]
  [string]   $StorageAccountKey,

  [Parameter(Mandatory=$false)]
  [PSObject[]] $AdditionDataLocationList = $null
)
  • ServiceName:云服务名称。ServiceName: Your cloud service name.

  • Roles:角色列表,例如“WebRole1”或“WorkerRole1”。Roles: A list of roles, such as "WebRole1" or "WorkerRole1".

  • Instances:逗号分隔的角色实例名称列表 — 使用通配符字符串(“*”)代表所有角色实例。Instances: A list of the names of role instances separated by comma -- use the wildcard string ("*") for all role instances.

  • Slot:槽名称。Slot: Slot name. “生产”或“过渡”。"Production" or "Staging".

  • Mode:收集模式。Mode: Collection mode. “完整”或“GA”。"Full" or "GA".

  • StorageAccountName:用于存储所收集数据的 Azure 存储帐户的名称。StorageAccountName: Name of Azure storage account for storing collected data.

  • StorageAccountKey:Azure 存储帐户密钥的名称。StorageAccountKey: Name of Azure storage account key.

  • AdditionalDataLocationList:以下结构的列表:AdditionalDataLocationList: A list of the following structure:

    {
    String Name,
    String Location,
    String SearchPattern,
    Bool   Recursive
    }
    

作为 VM 扩展添加Adding as a VM Extension

按照说明将 Azure PowerShell 连接到订阅。Follow the instructions to connect Azure PowerShell to your subscription.

  1. 指定服务名称、VM 和收集模式。Specify the service name, VM, and the collection mode.

    #Specify your cloud service name
    $ServiceName = 'YourCloudServiceName'
    
    #Specify the VM name
    $VMName = "'YourVMName'"
    
    #Specify the collection mode, "Full" or "GA"
    $mode = "GA"
    
    Specify the additional data folder for which files will be collected (this step is optional).
    
    #add one location
    $a1 = New-Object PSObject
    
    $a1 | Add-Member -MemberType NoteProperty -Name "Name" -Value "StorageData"
    $a1 | Add-Member -MemberType NoteProperty -Name "SearchPattern" -Value "*"
    $a1 | Add-Member -MemberType NoteProperty -Name "Location" -Value "%roleroot%storage"  #%roleroot% is normally E: or F: drive
    $a1 | Add-Member -MemberType NoteProperty -Name "Recursive" -Value "true"
    
    $AdditionalDataList+= $a1
        #more locations can be added....
    
  2. 提供要向其上传所收集文件的 Azure 存储帐户名称和密钥。Provide the Azure storage account name and key to which collected files will be uploaded.

    $StorageAccountName = 'YourStorageAccountName'
    $StorageAccountKey  = 'YourStorageAccountKey'
    
  3. 按如下所示调用 SetAzureVMLogCollector.ps1(本文末尾提供),以便为云服务启用 AzureLogCollector 扩展。Call the SetAzureVMLogCollector.ps1 (included at the end of the article) as follows to enable the AzureLogCollector extension for a Cloud Service. 执行完以后,可以在 https://YourStorageAccountName.blob.core.chinacloudapi.cn/vmlogs 下找到上传的文件Once the execution is completed, you can find the uploaded file under https://YourStorageAccountName.blob.core.chinacloudapi.cn/vmlogs

下面是传递给脚本的参数的定义。The following is the definition of the parameters passed to the script. (也在下面复制。)(This is copied below as well.)

[CmdletBinding(SupportsShouldProcess = $true)]

param (
  [Parameter(Mandatory=$true)]
  [string]   $ServiceName,

  [Parameter(Mandatory=$false)]
  [string] $VMName ,

  [Parameter(Mandatory=$false)]
  [string]   $Mode = 'Full',

  [Parameter(Mandatory=$false)]
  [string]   $StorageAccountName,

  [Parameter(Mandatory=$false)]
  [string]   $StorageAccountKey,

  [Parameter(Mandatory=$false)]
  [PSObject[]] $AdditionDataLocationList = $null
)
  • ServiceName:云服务名称。ServiceName: Your cloud service name.

  • VMName:VM 的名称。VMName: The name of the VM.

  • Mode:收集模式。Mode: Collection mode. “完整”或“GA”。"Full" or "GA".

  • StorageAccountName:用于存储所收集数据的 Azure 存储帐户的名称。StorageAccountName: Name of Azure storage account for storing collected data.

  • StorageAccountKey:Azure 存储帐户密钥的名称。StorageAccountKey: Name of Azure storage account key.

  • AdditionalDataLocationList:以下结构的列表:AdditionalDataLocationList: A list of the following structure:

    {
      String Name,
      String Location,
      String SearchPattern,
      Bool   Recursive
    }
    

扩展 PowerShell 脚本文件Extention PowerShell Script files

SetAzureServiceLogCollector.ps1SetAzureServiceLogCollector.ps1

[CmdletBinding(SupportsShouldProcess = $true)]

param (
  [Parameter(Mandatory=$true)]
  [string]   $ServiceName,

  [Parameter(Mandatory=$false)]
  [string[]] $Roles ,

  [Parameter(Mandatory=$false)]
  [string[]] $Instances = '*',

  [Parameter(Mandatory=$false)]
  [string]   $Slot = 'Production',

  [Parameter(Mandatory=$false)]
  [string]   $Mode = 'Full',

  [Parameter(Mandatory=$false)]
  [string]   $StorageAccountName,

  [Parameter(Mandatory=$false)]
  [string]   $StorageAccountKey,

  [Parameter(Mandatory=$false)]
  [PSObject[]] $AdditionDataLocationList = $null
)

$publicConfig = New-Object PSObject

if ($Instances -ne $null -and $Instances.Count -gt 0)  #Instances should be separated by ,
{
  $instanceText = $Instances[0]
  for ($i = 1;$i -lt $Instances.Count;$i++)
  {
    $instanceText = $instanceText+ "," + $Instances[$i]
  }
  $publicConfig | Add-Member -MemberType NoteProperty -Name "Instances" -Value $instanceText
}
else  #For all instances if not specified.  The value should be a space or *
{
  $publicConfig | Add-Member -MemberType NoteProperty -Name "Instances" -Value " "
}

if ($Mode -ne $null )
{
  $publicConfig | Add-Member -MemberType NoteProperty -Name "Mode" -Value $Mode
}
else
{
  $publicConfig | Add-Member -MemberType NoteProperty -Name "Mode" -Value "Full"
}

#
#we need to get the Sasuri from StorageAccount and containers
#
$context = New-AzStorageContext -Environment AzureChinaCloud -Protocol https -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

$ContainerName = "azurelogcollectordata"
$existingContainer = Get-AzStorageContainer -Context $context |  Where-Object { $_.Name -like $ContainerName}
if ($existingContainer -eq $null)
{
  "Container ($ContainerName) doesn't exist. Creating it now.."
  New-AzStorageContainer -Context $context -Name $ContainerName -Permission off
}

$ExpiryTime =  [DateTime]::Now.AddMinutes(120).ToString("o")
$SasUri = New-AzStorageContainerSASToken -ExpiryTime $ExpiryTime -FullUri -Name $ContainerName -Permission rwl -Context $context
$publicConfig | Add-Member -MemberType NoteProperty -Name "SasUri" -Value $SasUri

#
#Add AdditionalData to collect data from additional folders
#
if ($AdditionDataLocationList -ne $null )
{
  $publicConfig | Add-Member -MemberType NoteProperty -Name "AdditionalData" -Value $AdditionDataLocationList
}

#
# Convert it to JSON format
#
$publicConfigJSON = $publicConfig | ConvertTo-Json
"publicConfig is:  $publicConfigJSON"

#we just provide an empty privateConfig object
$privateconfig = "{
}"

if ($Roles -ne $null)
{
  Set-AzureServiceExtension -Service $ServiceName -Slot $Slot -Role $Roles -ExtensionName 'AzureLogCollector' -ProviderNamespace Microsoft.WindowsAzure.Compute -PublicConfiguration $publicConfigJSON -PrivateConfiguration $privateconfig -Version 1.0 -Verbose
}
else
{
  Set-AzureServiceExtension -Service $ServiceName -Slot $Slot  -ExtensionName 'AzureLogCollector' -ProviderNamespace Microsoft.WindowsAzure.Compute -PublicConfiguration $publicConfigJSON -PrivateConfiguration $privateconfig -Version 1.0 -Verbose
}

#
#This is an optional step: generate a sasUri to the container so it can be shared with other people if needed.
#
$SasExpireTime = [DateTime]::Now.AddMinutes(120).ToString("o")
$SasUri = New-AzureStorageContainerSASToken -ExpiryTime $ExpiryTime -FullUri -Name $ContainerName -Permission rl -Context $context
$SasUri = $SasUri + "&restype=container&comp=list"
Write-Output "The container for uploaded file can be accessed using this link:`r`n$sasuri"

SetAzureVMLogCollector.ps1SetAzureVMLogCollector.ps1

[CmdletBinding(SupportsShouldProcess = $true)]

param (
              [Parameter(Mandatory=$true)]
              [string]   $ServiceName,

              [Parameter(Mandatory=$false)]
              [string] $VMName ,

              [Parameter(Mandatory=$false)]
              [string]   $Mode = 'Full',

              [Parameter(Mandatory=$false)]
              [string]   $StorageAccountName,

              [Parameter(Mandatory=$false)]
              [string]   $StorageAccountKey,

              [Parameter(Mandatory=$false)]
              [PSObject[]] $AdditionDataLocationList = $null
        )

$publicConfig = New-Object PSObject
$publicConfig | Add-Member -MemberType NoteProperty -Name "Instances" -Value "*"

if ($Mode -ne $null )
{
    $publicConfig | Add-Member -MemberType NoteProperty -Name "Mode" -Value $Mode
}
else
{
    $publicConfig | Add-Member -MemberType NoteProperty -Name "Mode" -Value "Full"
}

#
#we need to get the Sasuri from StorageAccount and containers
#
$context = New-AzStorageContext -Environment AzureChinaCloud -Protocol https -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey

$ContainerName = "azurelogcollectordata"
$existingContainer = Get-AzStorageContainer -Context $context |  Where-Object { $_.Name -like $ContainerName}
if ($existingContainer -eq $null)
{
    "Container ($ContainerName) doesn't exist. Creating it now.."
    New-AzStorageContainer -Context $context -Name $ContainerName -Permission off
}

$ExpiryTime =  [DateTime]::Now.AddMinutes(90).ToString("o")
$SasUri = New-AzStorageContainerSASToken -ExpiryTime $ExpiryTime -FullUri -Name $ContainerName -Permission rwl -Context $context
$publicConfig | Add-Member -MemberType NoteProperty -Name "SasUri" -Value $SasUri

#
#Add AdditionalData to collect data from additional folders
#
if ($AdditionDataLocationList -ne $null )
{
  $publicConfig | Add-Member -MemberType NoteProperty -Name "AdditionalData" -Value $AdditionDataLocationList
}

#
# Convert it to JSON format
#
$publicConfigJSON = $publicConfig | ConvertTo-Json

Write-Output "PublicConfiguration is: \r\n$publicConfigJSON"

#
#we just provide an empty privateConfig object
#
$privateconfig = "{
}"

if ($VMName -ne $null )
{
      $VM = Get-AzureVM -ServiceName $ServiceName -Name $VMName
      $VM.VM.OSVirtualHardDisk.OS

      if ($VM.VM.OSVirtualHardDisk.OS -like '*Windows*')
      {
            Set-AzureVMExtension -VM $VM -ExtensionName "AzureLogCollector" -Publisher Microsoft.WindowsAzure.Compute -PublicConfiguration $publicConfigJSON -PrivateConfiguration $privateconfig -Version 1.* | Update-AzureVM -Verbose

            #
            #We will check the VM status to find if operation by extension has been completed or not. The completion of the operation,either succeed or fail, can be indicated by
            #the presence of SubstatusList field.
            #
            $Completed = $false
            while ($Completed -ne $true)
            {
                    $VM = Get-AzureVM -ServiceName $ServiceName -Name $VMName
                    $status = $VM.ResourceExtensionStatusList | Where-Object {$_.HandlerName -eq "Microsoft.WindowsAzure.Compute.AzureLogCollector"}

                    if ( ($status.Code -ne 0) -and ($status.Status -like '*error*'))
                    {
                        Write-Output "Error status is returned: $($Status.ExtensionSettingStatus.FormattedMessage.Message)."
                          $Completed = $true
                    }
                    elseif (($status.ExtensionSettingStatus.SubstatusList -eq $null -or $status.ExtensionSettingStatus.SubstatusList.Count -lt 1))
                    {
                          $Completed = $false
                          Write-Output "Waiting for operation to complete..."
                    }
                    else
                    {
                          $Completed = $true
                          Write-Output "Operation completed."

                    $UploadedFileUri = $Status.ExtensionSettingStatus.SubStatusList[0].FormattedMessage.Message
                          $blob = New-Object Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob($UploadedFileUri)

                  #
                        # This is an optional step:  For easier access to the file, we can generate a read-only SasUri directly to the file
                          #
                          $ExpiryTimeRead =  [DateTime]::Now.AddMinutes(120).ToString("o")
                          $ReadSasUri = New-AzStorageBlobSASToken -ExpiryTime $ExpiryTimeRead  -FullUri  -Blob  $blob.name -Container $blob.Container.Name -Permission r -Context $context

                        Write-Output "The uploaded file can be accessed using this link: $ReadSasUri"

                          #
                          #This is an optional step:  Remove the extension after we are done
                          #
                          Get-AzureVM -ServiceName $ServiceName -Name $VMName | Set-AzureVMExtension -Publisher Microsoft.WindowsAzure.Compute -ExtensionName "AzureLogCollector" -Version 1.* -Uninstall | Update-AzureVM -Verbose

                    }
                    Start-Sleep -s 5
            }
      }
      else
      {
          Write-Output "VM OS Type is not Windows, the extension cannot be enabled"
      }

}
else
{
  Write-Output "VM name is not specified, the extension cannot be enabled"
}

后续步骤Next Steps

现在,可以从一个简单的位置查看或复制日志。Now you can examine or copy your logs from one simple location.