问题
We had quite a big issue with our azure web app service. All of a sudden we started getting 'disk out of space' errors. Sure enough, the LogFiles directory was huge. We weren't sure how to clear this directory. After a while and without finding any recommended way of doing it, we figured that it was just log files and it was probably safe to just delete a few files in there.
Via the kudu powershell console, we deleted a dump file LogFile/dumps and we also deleted a bunch a stdout files via a rm stdout_*.log
command.
Shockingly, this completely screwed our slot!
We tried to redeploy but we get getting HTTP 503 errors and there was nothing obvious to us on how to fix this.
Luckily, we deleting these files on the staging slot and not production so there was no downtime. We ended spinning a brand new slot and deploying there, and then deleting the previous slot.
Not a great experience for sure. Can anyone enlighten me about what might have happened? We have a pretty simple asp.net core 2.1 app.
Can deleting log files really mess up a slot???!
回答1:
Can deleting log files really mess up a slot?
No. You might delete some config file which caused slot app not work.
You could use the following code to delete 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
Output:
回答2:
One way to resolve this, is to create a CRON triggered Web Job that runs on a daily or weekly basis. Web Jobs run on the same App Service plan which means you can use simple PowerShell script to delete old log-files created by your App Service. Here's an example:
$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;
I ran into storage issues on Azure App Service with massive log-files too and decided to document the process in more details in this blog post: "Deleting old web app logs using Azure Web Jobs and PowerShell".
Sidenote: some logging libraries have built-in support for auto-removing old log files which would achieve the same as described above
来源:https://stackoverflow.com/questions/52800853/how-to-clean-up-azure-web-app-logfiles-directory