如何清理 azure web 应用程序 LogFiles 目录?
How to clean up azure web app LogFiles directory?
我们的 Azure Web 应用程序服务遇到了很大的问题。
突然之间,我们开始收到 'disk out of space' 错误。
果然,LogFiles 目录很大。
我们不确定如何清除此目录。
一段时间后,在没有找到任何推荐的方法的情况下,我们认为它只是日志文件,删除其中的几个文件可能是安全的。
通过 kudu powershell 控制台,我们删除了一个转储文件 LogFile/dumps,我们还通过 rm stdout_*.log
命令删除了一堆标准输出文件。
令人震惊的是,这完全搞砸了我们的位置!
我们尝试重新部署,但收到 HTTP 503 错误,我们没有明显的解决方法。
幸运的是,我们删除了暂存槽中的这些文件而不是生产中的文件,因此没有停机时间。
我们结束了旋转一个全新的插槽并在那里部署,然后删除了以前的插槽。
当然不是很好的体验。
谁能告诉我可能发生了什么?
我们有一个非常简单的 asp.net 核心 2.1 应用程序。
删除日志文件真的会弄乱插槽吗???!
Can deleting log files really mess up a slot?
没有。您可能会删除一些导致老虎机应用程序无法运行的配置文件。
您可以使用以下代码删除 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
输出:
解决此问题的一种方法是创建一个 CRON 触发的 Web 作业,运行 每天或每周[=22] =].
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 App Service 上使用大量日志文件解决存储问题,并决定在此博客中更详细地记录该过程 post:"Deleting old web app logs using Azure Web Jobs and PowerShell".
旁注:一些日志库内置了对自动删除旧日志文件的支持,这将实现与上述相同的功能
我们的 Azure Web 应用程序服务遇到了很大的问题。 突然之间,我们开始收到 'disk out of space' 错误。 果然,LogFiles 目录很大。 我们不确定如何清除此目录。 一段时间后,在没有找到任何推荐的方法的情况下,我们认为它只是日志文件,删除其中的几个文件可能是安全的。
通过 kudu powershell 控制台,我们删除了一个转储文件 LogFile/dumps,我们还通过 rm stdout_*.log
命令删除了一堆标准输出文件。
令人震惊的是,这完全搞砸了我们的位置!
我们尝试重新部署,但收到 HTTP 503 错误,我们没有明显的解决方法。
幸运的是,我们删除了暂存槽中的这些文件而不是生产中的文件,因此没有停机时间。 我们结束了旋转一个全新的插槽并在那里部署,然后删除了以前的插槽。
当然不是很好的体验。 谁能告诉我可能发生了什么? 我们有一个非常简单的 asp.net 核心 2.1 应用程序。
删除日志文件真的会弄乱插槽吗???!
Can deleting log files really mess up a slot?
没有。您可能会删除一些导致老虎机应用程序无法运行的配置文件。
您可以使用以下代码删除 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
输出:
解决此问题的一种方法是创建一个 CRON 触发的 Web 作业,运行 每天或每周[=22] =]. 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 App Service 上使用大量日志文件解决存储问题,并决定在此博客中更详细地记录该过程 post:"Deleting old web app logs using Azure Web Jobs and PowerShell".
旁注:一些日志库内置了对自动删除旧日志文件的支持,这将实现与上述相同的功能