代码之家  ›  专栏  ›  技术社区  ›  Clement

如何清理azure web app日志文件目录?

  •  1
  • Clement  · 技术社区  · 6 年前

    我们不知道如何清除这个目录。 过了一段时间,没有找到任何推荐的方法,我们认为这只是日志文件,删除其中的一些文件可能是安全的。

    通过kudu powershell控制台,我们删除了一个转储文件LogFile/dumps,还通过 rm stdout_*.log 命令。 令人震惊的是,这完全毁了我们的机会!

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

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

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Joey Cai    6 年前

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

    您可以使用以下代码删除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
    

    输出: enter image description here

        2
  •  0
  •   Swimburger    5 年前

    继续下去 每天或每周 . 运行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" .