Steps to enable log rotation & delete old log files in IIS


Please follow the instructions below to configure log rotation in IIS.

1. Login to the server.

2. Open IIS Manager and select the required site.

3. In Features View, double-click "Logging".


4. Under Logging pane, in the Log File Rollover section, select the appropriate condition to create a new log file. It is recommended that you schedule the new log file creation on a daily basis by selecting "daily" in the "schedule" option.



5. Select "Use local time for file naming and rollover" to specify that log file naming and time for log file rollover is using the correct local server time.

6. Click Apply in the Actions pane to save your changes.

Deleting the Log file:

1. Copying the script below into notepad and save it as a Log_File_Deletion.vbs.

=====================================================================
sLogFolder = "c:\inetpub\logs\LogFiles"
iMaxAge = 30   'in days
Set objFSO = CreateObject("Scripting.FileSystemObject")
set colFolder = objFSO.GetFolder(sLogFolder)
For Each colSubfolder in colFolder.SubFolders
        Set objFolder = objFSO.GetFolder(colSubfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
                iFileAge = now-objFile.DateCreated
                if iFileAge > (iMaxAge+1)  then
                        objFSO.deletefile objFile, True
                end if
        Next
Next
=======================================================================

The path where log files are saved, can be found out from IIS Manager >> IIS features >> Logging >> Directory

 

To run the script as a scheduled task, we can have windows task schedule run the script periodically. To schedule task to run the script please follow the steps below: 

1. Open Server Manager, click the "configuration" menu, and then click "Task Scheduler".



2. In the Actions pane of the Task Scheduler dialog box, click "Create Task".



3. On the General tab of the Create Task dialog box, enter a name for the task, such as "Delete Log Files". To set the security properties, click on the button marked Change User or Group next to the security options area of the dialog. This allows us to specify what user the task will run as. Enter the user SYSTEM and click "OK".

4. Click "OK" in the General tab.

5. Click the Triggers tab, and then click "New". In the New Trigger dialog box, set Begin the task to On a schedule. Select the periodicity, for example, Daily. Enter the Start date, select more advanced settings, and ensure that Enabled is selected if you are ready to initiate the schedule. Click "OK".



6. Click the Actions tab, and then click "New". In the New Action dialog box, select a value for Action, in this case, Start a program. In Program/script, enter cscript, and in Add arguments (optional), enter the path and name of the script file, for example, "C:\iis\Log_File_Deletion.vbs". Click OK.



7. Click "OK" in the Actions tab.

8. Verify that the task has been added to the Active Tasks pane. To verify, scroll down to see "Active Tasks" in the task scheduler pane and double click the task we have created.



9. Right-click on the new task we created now, and select Run.

10. Navigate to the folder that the script ran on (c:\inetpub\logs\LogFiles), and verify that the appropriate log files were deleted.

11. Navigate back to the Task Scheduler, right-click on the task, and click End so the status returns to Ready and the task is ready for scheduled runs.





Last update:
2016-02-10 15:28
Author:
Bytehouse Technical Support
Revision:
1.5
Average rating: 4.2 (5 Votes)

You can comment this FAQ

Chuck Norris has counted to infinity. Twice.

top