Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've used Apache for years and I've taken for granted that it handles log rotation for me. I've been digging through the IIS config and googling, but I can't find an option for IIS to turn on log rotation.

What is the preferred way to configure IIS to delete logs past a certain threshold? Are there any products out there that do this for you? What do enterprisy Windows shops do?

Configuration: IIS 6.0 / Windows Server 2003 32-bit

share|improve this question
    
A very good question. IIS does have rotation, but does not handle purging, nor archiving. I do not understand why a product, so mature in other areas, would be missing such a basic feature. With the default configuration, IIS commits suicide by filling the root drive with logs. (I guess the same can be said for SQL and its transaction logs.) –  Nathan Hartley Mar 10 at 19:20

6 Answers 6

up vote 6 down vote accepted

There's no built in mechanism to handle log rotation or archiving. There may be third party products to handle this chore or you could script something and configure a scheduled task. I have only a handfull of IIS servers so I just set up a recurring Task in Outlook to remind me monthly to delete IIS log files older than 30 days.

share|improve this answer
3  
That sounds painful. Try scheduling this... Get-ChildItem E:\log* -Include ex*.log -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-30)} | Remove-Item –  Nathan Hartley Mar 10 at 21:18

Check: http://www.808.dk/?code-iis-log-housekeeping

A bunch of scripts are available, which you can add in the scheduled task = logrotation :)

share|improve this answer

Have a look at the IIS Logs tool (http://www.iislogs.com/). There are a couple of different ways to install this and does a very effective job at managing IIS and other log files (compress files to .zip format, move them to different locations, delete files past a certain date, etc.).

share|improve this answer

The other answers are good, and offer a more robust solution to this problem. If you just need a quick fix, you can setup CCleaner to automatically clean your logs folder each time you login, or on a schedule.

Keep in mind, though, that this deletes all logs, not just aging files.

I followed these instructions but added that I wanted it to run even when the user was logged off, as is the normal case on a server.

share|improve this answer

Setup a scheduled task to use robocopy to copy the files to a subdirectory named "old". It has a switch, /minage:x that you can set to 30,60, or whatever you feel. Then delete everything in that directory. I'm doing it this way on a couple dozen servers, and it seems to do the trick.

share|improve this answer

I recently posed this same question on the Powershell.org forum. Then ended up posting a hack of a Script Resource for Desired State Configuration, that I will be using to solve my need for a log purging routine. Maybe some of this code will be useful to others.

# Requires an E: drive.

configuration LogDirectory
{
    param (
        [Parameter(Mandatory)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Node
    )
    node $Node
    {
        Script LogDirectoryScript
        {
            GetScript = {
                $result = (Test-Path 'E:\log') -and (schtasks.exe /query /TN Purge_Log_Folder | Select-String Purge_Log_Folder -Quiet)
                return @{
                    GetScript = $GetScript
                    SetScript = $SetScript
                    TestScript = $TestScript
                    Result = $result
                }
            }

            SetScript = {
                Write-Verbose 'Creating log directory.'
                if ( -not (Test-Path 'E:\log')) { 
                    New-Item -ItemType Directory -Path 'E:\log'
                }

                Write-Verbose 'Changing permissions to log directory such that any user can write, but only an administrator can read or modify.'
                $acl = (Get-Item 'E:\log').GetAccessControl('Access')
                $acl.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule('Users','Write', 'Allow')))
                Set-Acl -Path 'E:\log' -AclObject $acl | Out-Host

                Write-Verbose 'Scheduling purge task.'
                $script = 'Get-ChildItem E:\log\* -Include ex*.log -Recurse  | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item'
                Set-Content -Path C:\Windows\Purge_Log_Folder.ps1 -Value $script
                $task = 'Powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File C:\Windows\Purge_Log_Folder.ps1'
                SCHTASKS /CREATE /TN Purge_Log_Folder /TR $task /SC DAILY /ST 23:59 /RU SYSTEM /F | Out-Host

                Write-Verbose 'Configuring IIS'
                # Default Log File Settings for Web Sites <logFile>
                # http://www.iis.net/configreference/system.applicationhost/sites/sitedefaults/logfile
                Import-Module WebAdministration
                Set-WebConfigurationProperty '/system.applicationHost/sites/siteDefaults' -name logFile -value @{
                    directory = 'E:\log'
                    localTimeRollover ='true'
                    period = 'Hourly'
                }
            }

            TestScript = {
                (Test-Path 'E:\log') -and (schtasks.exe /query /TN Purge_Log_Folder | Select-String Purge_Log_Folder -Quiet)
            }
        }
    }
} 
LogDirectory
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.