Swimburger

Deleting old web app logs using Azure Web Jobs and PowerShell

Niels Swimberghe

Niels Swimberghe - - Azure

Follow me on Twitter, buy me a coffee

PowerShell and Azure WebJobs Logo running a shredder, shredding logs

When your Azure App Service writes lots of logs, these logs can quickly pile up and even reach your "File system storage" quota limits.
This was something I personally didn't pay attention to for quite some time and was surprised to find multiple gigabytes of logs sitting in my app service.
To solve this issue, you can use a PowerShell script and a time triggered Azure Web Job.

Note: This post uses Windows as the Operating System of the Azure App Service. Steps will vary for Linux OS.

Creating a PowerShell script to delete old log files #

Azure Web Jobs supports many languages: Windows cmd & exe, PowerShell, Bash, PHP, Python, NodeJS, and Java.
PowerShell is a really good scripting language to automate many things on both Linux and Windows. You can use the following one-liner PowerShell script to delete files that are older than 30 days.

$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

Creating an Azure Web Job to delete old log files #

Now that you're script is ready, you can create an Azure Web Job under you Azure App Service using the following steps:

1. Navigate to the Web Jobs section under your Azure App Service and click "Add".

Add Azure Web Job

2. Fill out the Add WebJob as shown below in the screenshot:

Create Clean Logs Azure Web Job

WebJob properties:

  • Name: Give the WebJob a name that doesn't conflict with other web jobs in your app service.
  • File Upload: Upload the PowerShell script file
  • Type: Use triggered so you can setup a CRON schedule to run the script on an interval basis.
  • CRON Expression: Enter a valid CRON expression which specifies on what time interval to execute the script.
    The CRON expression for Web Jobs is using the open-source project NCrontab. Refer to NCrontab on GitHub to learn how to build the CRON expression.
    You can also test out NCrontab expressions online at ncrontab.swimburger.net.
    The WebJob in the screenshot uses "0 0 0 * * *" which is the equivalent of running the script every day at midnight.

3. Hit OK and you'll be all set.

To test out the WebJob, you can hit "Run" from the Azure Portal interface.

Warning: Since you're deleting files using a script, it's a good idea to test this out in a safe environment before adding it to your production environment.

That's how you setup an Azure WebJob to delete your old log files using PowerShell, cheers!

Related Posts

Related Posts