Applies to version: Any; Author: Karol Woźniak
Log files taking up most of the space on a system disk is a common occurrence when dealing with IIS and SharePoint platform. Although many different solutions exist to counter this problem, each one must be individually tailored to the circumstances. The environment you are dealing with (e.g. DEV/TEST/PROD) will be a key issue that when determining your approach to the problem of growing log files.
In addition to some general tips, I’d like to also present you with a PowerShell solution that, when combined with Windows task automation, can keep your disk space free from older useless logs.
IMPORTANT: Keep in mind that approach presented below is mostly aimed at development environments and certain test environments, and thus should be handled with additional care and caution on production servers.
Compressing Internet Information Services (IIS) logs
By default, IIS stores its logs under the %SystemDrive%\inetpub\logs\LogFiles path. Automating the process of removing old log files is not available through IIS directly, however there are some practices that can help in reducing disk space allocated by it.
The easiest way is to enable contents compression on a directory containing log files. To do so just open folder Properties by right-clicking on it and on the General tab of properties page click Advanced.
The Advanced Attributes window should appear, check the “Compress contents to save disk space” box.
SharePoint logs management
Unlike IIS, SharePoint does provide a way to manage log files out of the box. Such options are accessible from SharePoint Central Administration. They can be found under the Monitoring category and then clicking “Configure diagnostic logging”
There are several options available here:
- Changing default path where trace logs are stored
- Deciding on number of days to store log files (14 by default)
- Restrict trace log disk space usage (disabled by default)
Administrators can (and should!) utilize these options to store log files according to company policy, good practices and common sense.
PowerShell solution for IIS & SharePoint
Using PowerShell to address the problems highlighted above is a more universal solution for both ISS and SharePoint. Combined with Windows Task Scheduler, it gives much more flexibility for the Administrator to decide when and under what conditions such a script should be executed, and hence clean the log files.
We can also provide the same set of rules (e.g. how old should files be before deletion) for both types of log files and reuse such a solution for many different environments/servers.
Download the script by clicking here:
What the script does:
- Checks SharePoint version (Sp2010/2013/2016) and sets a default log files path
- Tries to remove all files with .log extensions from default IIS log folder and determined SharePoint logs folder
- It will remove only .log files older than N days (where N is a parameter sent when you invoke a function e.g. -30 means files older than 30 days)
- Three parameters needs to be provided when invoking a script: number of days (files older than this value will be removed), directory path where to save script log file, name of log file
The recommend method of using this solution is with Task Scheduler.
- Open Windows Task Scheduler
- Add new task
- Configure General tab [example on screenshot below]:
- Configure triggers according to your preferences [example on screenshot below]:
- Configure Actions: in this step the PowerShell script will be invoked with appropriate parameters
Remember about passing correct arguments when invoking the script, e.g.:
“C:\Users\k.wozniak\Desktop\clearSpandIISLogs.ps1” -10 “C:\logFiles\” “cleanLog.txt”
which in general is:
“path to script” numberOfDays “path to save script log file” “name of script log with extension”
numberOfDays – if set to e.g. -30 means that only log files older than 30 days will be removed
- Configuration of other steps is left up to Administrator
Drawbacks of the PowerShell script approach:
- Although the function will check the SharePoint version, it works only for default log save path. If the administrator changed it to something custom in Central Administrator SharePoint logs will not be cleared
- If any of the log files that are being removed are accessed by another process, an error will be thrown into the console (but the function will keep removing other files regardless).