Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ ms.custom: sfi-image-nochange
---
# Managing IIS Log File Storage

by [Jim van de Erve](https://twitter.com/jimvde)
by [Jim van de Erve](https://twitter.com/jimvde) and [Nash Pherson](https://github.com/npherson)

You can manage the amount of server disk space that Internet Information Services (IIS) log files consume by using compression, remote storage, and scripted deletion.
You can manage the amount of server disk space that Internet Information Services (IIS) log files consume by using compression, remote storage, and scripted deletion of aged log files.

## Overview

The log files that IIS generates can, over time, consume a large amount of disk space. Logs can potentially fill up an entire hard drive. To mitigate this problem, many users turn off logging completely. Fortunately, there are alternatives to doing so, such as the following:
The log files IIS generates can, over time, consume a large amount of disk space. IIS does not have any native funcationality for log trimming or rollover, which means logs can potentially fill up an entire drive. To mitigate disk usage issues, consider the following options:

- [Enable folder compression](#00)
- [Move the log folder to a remote system](#01)
- [Delete old log files by script.](#02)
- [Delete old log files - PowerShell Example](#02)
- [Delete old log files - VBScript Example](#03)

The above mitigations are described in the sections below. You may also want to do the following to control disk usage:

Expand All @@ -33,7 +34,7 @@ For more information, see [Configuring Logging in IIS](configure-logging-in-iis.
<a id="00"></a>
## Enable Folder Compression

IIS log files compress to about 2% of their original size. Enable compression of a log file as follows. You must be an administrator to perform this procedure.
IIS log files are text which is highly compressable; they can shrink to about 2% of their original size. With administrator permissions, you can enable compression of a log file as follows.

1. Click the **File Manager** icon in the icon bar.
2. Move to the folder containing IIS log files (by default, `%SystemDrive%\inetpub\logs\LogFiles`).
Expand All @@ -44,7 +45,7 @@ IIS log files compress to about 2% of their original size. Enable compression of
6. Click **Apply**, and then select whether to compress the folder only, or the folder, its subfolders, and its files.
7. Click **OK**. Verify that the folder contents are compressed. The name of the folder and the name of each file should be colored blue, and the size of a compression file should be smaller.

This is a simple way to lower disk usage. It is not a final solution, however, because disk usage still grows over time, and could eventually fill up the hard drive.
This is a simple way to lower disk usage. It is not a final solution, however, because disk usage still grows over time, and could eventually fill up the drive.

If the folder already contains a significant amount of data, it could take the computer a while to compress its contents. Also note that this one-time process could slow down the computer during the initial compression, so if this is a production server, perform this operation during off-peak hours to prevent service degradation.

Expand Down Expand Up @@ -73,9 +74,26 @@ Change the location of an IIS log file to a remote share as follows:
7. In the **Actions** pane, click **Apply**, and then click **OK**. All Web sites within the directory should begin logging data to the remote share.

For more information, see [Remote Logging](https://technet.microsoft.com/library/cc786172(v=ws.10).aspx).

<a id="02"></a>
## Delete Old Log Files by Script
## Delete Old Log Files by PowerShell Script

You can use a PowerShell script periodicaly to delete log files older than a specified number of days. While you can manually run the script, you could create a scheduled task to run every day so aged logs are always deleted.

This example script will search all subfolders under the path specified in $logPath for .log files older than the number of days specified in $daysToKeep and **delete them**.

[!code-powershell[Main](managing-iis-log-file-storage/samples/sample1.ps1)]

You can manually create a scheduled task in Task Scheduler or from an elevated PowerShell prompt. For example, if the script was saved to C:\inetpub\logs\IISCleanup.ps1, you could use the following commands to create a scheduled task that runs the script daily at 2:00AM as system:
```powershell
$scriptPath = "C:\inetpub\logs\IISLogCleanup.ps1"
$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File $scriptPath"
$trigger = New-ScheduledTaskTrigger -Daily -At 2:00AM
Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "IIS Log CLeanup" -User "NT AUTHORITY\SYSTEM"
```

<a id="03"></a>
## Delete Old Log Files by VBScript

You can control disk usage of log files by running a script that automatically deletes log files that are older than a certain age. Running this script in a scheduled task will keep the problem of a disk filling up under control without constant maintenance.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
###########################################################################################################
# This is an example script for cleaning up old IIS Log Files designed to run as a scheduled task.
#
# Example powershell for scheduling the script to run every day at 2:00 AM as system. Adjust file path and user as appropriate.
# $scriptPath = "C:\inetpub\logs\IISLogCleanup.ps1"
# $action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File $scriptPath"
# $trigger = New-ScheduledTaskTrigger -Daily -At 2:00AM
# Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "IIS Log Cleanup" -User "NT AUTHORITY\SYSTEM"
###########################################################################################################


# Configure your settings...
$logPath = "C:\inetpub\logs\LogFiles"
$daysToKeep = 365

# Find old log files...
$dateToCompare = $(Get-Date).AddDays(-$daysToKeep)
$oldLogFiles = Get-ChildItem -Path $logPath -Filter "*.log" -Recurse | Where-Object { $_.LastWriteTime -lt $dateToCompare }
$measure = $oldLogFiles | Measure-Object -Property length -Sum

# Delete old log files...
$oldLogFiles | ForEach-Object {
Remove-Item $_.FullName -Force
}

# Report results...
Write-Host "Completed cleanup of log files older than $daysToKeep days. Attempted to remove" ($oldLogFiles.Count).ToString("n0") "files," ($measure.Sum / 1MB).ToString("n1") "MB in size."