We use (and love) Elmah. Due to many reasons we decided to opt for the XML file logging to AppData. This works great and is super simple to set up. The only issue is that on our test and production servers we were filling up the AppData directory and after awhile Elmah.axd would take minutes to load.
I wrote a Powershell script that we could put on a timer (Windows Schedule Task) that would delete all but the last 100 error logs. This is a pretty brute force script, but simple. Unfortunately due to some internal regulations we were not able to use the script so I’m sharing it here.
How to Use
Download the source below and save it as “prune.ps1”. Run it like this:
#> .\prune.ps1 -prunedirectory “c:\path\to\elmahlogs\”
The script will keep the 100 most recent. Do you want more kept around? No problem, just change the “$numberOfFilesToKeep” variable.
Enjoy –
param ( [string] $prunedirectory = $(throw "Parameter - 'prunedirectory' is required.") ) $numberOfFilesToKeep = 100 $files = get-childitem $prunedirectory*.xml | sort-object -property CreationTime -descending for($i = $numberOfFilesToKeep; $i -lt $files.count; $i++) { rm -force $files[$i].fullname }