0

Presently log in via Kudo / Azure CLI / FTP and manually delete.

We have an existing dev-ops codebase that uses Microsoft.Azure.Management. Looking for any way to delete files, specifically /LogFiles/*.log where older than x using C# vaguely.

Seemingly could enumerate WebApps and get FTP credentials and use FtpWebRequest but would like to know if there is a more direct way.

Did not see any methods here -> Web Apps:

ttugates
  • 5,818
  • 3
  • 44
  • 54

1 Answers1

4

I am afraid there is not a direct way. However,you can call Kudu Api to delete the log files. Below is an example of powershell script:

$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)

You can also refer to another example in this thread.

You can aslo create a azure web job and upload a powershell script to delete the old log files. Please check here for detailed steps.

Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43