【问题标题】:How to clean up azure web app LogFiles directory?如何清理 azure web app LogFiles 目录?
【发布时间】:2018-10-14 08:28:38
【问题描述】:

我们的 azure Web 应用服务存在相当大的问题。 突然间,我们开始出现“磁盘空间不足”错误。 果然,LogFiles 目录很大。 我们不确定如何清除此目录。 过了一段时间,没有找到任何推荐的方法,我们认为它只是日志文件,删除其中的几个文件可能是安全的。

通过 kudu powershell 控制台,我们删除了一个转储文件 LogFile/dumps,我们还通过 rm stdout_*.log 命令删除了一堆标准输出文件。 令人震惊的是,这完全搞砸了我们的插槽! 我们尝试重新部署,但遇到了 HTTP 503 错误,而且我们没有任何明显的解决方法。

幸运的是,我们在暂存槽而不是生产中删除了这些文件,因此没有停机。 我们结束了旋转一个全新的插槽并在那里部署,然后删除了之前的插槽。

肯定不是很棒的体验。 谁能告诉我可能发生了什么? 我们有一个非常简单的 asp.net core 2.1 应用程序。

删除日志文件真的会搞砸槽吗???!

【问题讨论】:

  • 事实证明,由于某种原因,我们的 web.config 中有 stdoutLogEnabled="true",这导致大量日志被写入并填满磁盘。

标签: azure azure-web-app-service azure-app-service-plans


【解决方案1】:

删除日志文件真的会搞砸槽吗?

没有。您可能会删除一些导致插槽应用程序无法运行的配置文件。

您可以使用以下代码删除 Web App LogFile。

$resourceGroupName="xxx"
$webAppName="xxxx"
$slotName="xxxxx"
function Get-AzureRmWebAppPublishingCredentials($resourceGroupName, $webAppName, $slotName = $null){
    if ([string]::IsNullOrWhiteSpace($slotName)){
        $resourceType = "Microsoft.Web/sites/config"
        $resourceName = "$webAppName/publishingcredentials"
    }
    else{
        $resourceType = "Microsoft.Web/sites/slots/config"
        $resourceName = "$webAppName/$slotName/publishingcredentials"
    }
    $publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force
    Write-Host $publishingCredentials   
    return $publishingCredentials
}

function Get-KuduApiAuthorizationHeaderValue($resourceGroupName, $webAppName, $slotName = $null){
    $publishingCredentials = Get-AzureRmWebAppPublishingCredentials $resourceGroupName $webAppName $slotName
    Write-Host $publishingCredentials.Properties.PublishingUserName
    Write-Host $publishingCredentials.Properties.PublishingPassword
    return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword))))
}

function Delete-WebAppLogFiles($resourceGroupName, $webAppName, $slotName = ""){

    $apiAuthorizationToken = Get-KuduApiAuthorizationHeaderValue $resourceGroupName $webAppName $slotName
    if ($slotName -eq ""){
        $apiUrl = "https://$webAppName.scm.azurewebsites.net/api/command"
    }
    else{
        $apiUrl = "https://$webAppName`-$slotName.scm.azurewebsites.net/api/command"
    }

    $apiCommand = @{
        #command='del *.* /S /Q /F'
        command = 'powershell.exe -command "Remove-Item -path d:\\home\\LogFiles\\* -recurse"'
        dir='d:\\home\\LogFiles'
    }

    Write-Output $apiUrl
    Write-Output $apiAuthorizationToken
    Write-Output $apiCommand
    Invoke-RestMethod -Uri $apiUrl -Headers @{"Authorization"=$apiAuthorizationToken;"If-Match"="*"} -Method POST -ContentType "application/json" -Body (ConvertTo-Json $apiCommand)

}

Delete-WebAppLogFiles $resourceGroupName $webAppName $slotName

输出:

【讨论】:

  • 我可以在本地机器上运行它吗?
  • 如何找到资源组名称?对于 webappname 应该是 url 吗?
  • 是的,您可以在本地 powershell 中运行它。对于资源组名称,您可以在 azure 上打开 Web 应用概览,您将获得资源组名称和 webappname。
【解决方案2】:

解决此问题的一种方法是创建 CRON 触发的 Web 作业,该作业每天或每周运行一次。 Web 作业在相同的应用服务计划上运行,这意味着您可以使用简单的 PowerShell 脚本来删除应用服务创建的旧日志文件。 这是一个例子:

$LogFolder = "D:\home\site\wwwroot\App_Data\Logs";
$DaysToKeepLogsAround = 30;
Get-ChildItem -Path $LogFolder -Recurse -File | Where LastWriteTime  -lt  (Get-Date).AddDays(-$DaysToKeepLogsAround) | Remove-Item -Force;

我在 Azure 应用服务上也遇到了大量日志文件的存储问题,并决定在这篇博文中更详细地记录该过程:"Deleting old web app logs using Azure Web Jobs and PowerShell"。

旁注:一些日志库内置了对自动删除旧日志文件的支持,可以实现与上述相同的效果

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 2016-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2017-12-07
    • 1970-01-01
    相关资源
    最近更新 更多