首页 > 解决方案 > 如何从 C# 或 Microsoft.Azure.Management 删除应用服务 -> Web 应用 -> 日志文件

问题描述

目前通过 Kudo / Azure CLI / FTP 登录并手动删除。

我们有一个现有的 dev-ops 代码库,它使用Microsoft.Azure.Management. 寻找任何方法来删除文件,特别是/LogFiles/*.log在使用 C# 模糊的 x 之前。

似乎可以枚举 WebApps 并获取 FTP 凭据并使用FtpWebRequest,但想知道是否有更直接的方法。

在这里没有看到任何方法 -> Web Apps

标签: azureazure-devopsazure-web-app-service

解决方案


恐怕没有直接的方法。但是,您可以调用Kudu Api来删除日志文件。下面是一个powershell脚本的例子:

$ResGroupName = ""
$WebAppName = ""
$LogFolder = ""
$DaysToKeepLogsAround=""

# Get publishing profile for web application

$WebApp = Get-AzWebApp -Name $WebAppName -ResourceGroupName $ResGroupName
[xml]$publishingProfile = Get-AzWebAppPublishingProfile -WebApp $WebApp

# Create Base64 authorization header

$username = $publishingProfile.publishData.publishProfile[0].userName
$password = $publishingProfile.publishData.publishProfile[0].userPWD
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$apiBaseUrl = "https://$($WebApp.Name).scm.azurewebsites.net/api/command"

# delete log files

 $apiCommand = @{
        command = 'powershell.exe -command "Get-ChildItem -Path $LogFolder -Recurse -File | Where LastWriteTime  -lt  (Get-Date).AddDays(-$DaysToKeepLogsAround) | Remove-Item -Force"'
        dir=$LogFolder
    }

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

您还可以参考此线程中的另一个示例。

您还可以创建一个azure web 作业并上传一个 powershell 脚本来删除旧的日志文件。请在此处查看详细步骤。


推荐阅读