如何清理azure web应用程序日志文件目录?

如何清理azure web应用程序日志文件目录?,azure,azure-web-app-service,azure-app-service-plans,Azure,Azure Web App Service,Azure App Service Plans,我们的azure web应用程序服务有一个很大的问题。 突然,我们开始出现“磁盘空间不足”错误。 果然,LogFiles目录很大。 我们不确定如何清除此目录。 过了一段时间,在没有找到任何推荐方法的情况下,我们认为这只是日志文件,删除其中的一些文件可能是安全的 通过kudu powershell控制台,我们删除了一个转储文件LogFile/dumps,还通过rm stdout.*.log命令删除了一堆stdout文件。 令人震惊的是,这完全毁了我们的饭碗! 我们试图重新部署,但我们得到了HTTP

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

通过kudu powershell控制台,我们删除了一个转储文件LogFile/dumps,还通过
rm stdout.*.log
命令删除了一堆stdout文件。 令人震惊的是,这完全毁了我们的饭碗! 我们试图重新部署,但我们得到了HTTP503错误,我们没有什么明显的关于如何修复这个问题

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

这肯定不是一次很好的经历。 有人能告诉我可能发生了什么事吗? 我们有一个非常简单的asp.net core 2.1应用程序

删除日志文件真的会弄乱插槽吗

删除日志文件真的会弄乱插槽吗

否。您可能会删除一些导致插槽应用程序无法工作的配置文件

您可以使用以下代码删除Web应用程序日志文件

$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
输出:

解决此问题的一种方法是创建一个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应用程序服务上也遇到了大量日志文件的存储问题,并决定在这篇博文中详细记录该过程:


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

我可以从本地计算机上运行它吗?我如何找到resroucegroupname?对于webappname,它应该是url吗?是的,您可以在本地powershell中运行它。对于resourcegroupname,您可以在azure上打开web应用的概述,您将获得resourcegroupname和webappname。事实证明,出于某种原因,我们的web.config中有stdoutLogEnabled=“true”,这导致大量日志被写入并填满磁盘。