Skip to main content
Jorge Bernhardt Jorge Bernhardt
  1. Posts/

How to configure IIS logging using PowerShell

·856 words·5 mins· 100 views · 5 likes ·
Get-ChildItem Get-ItemProperty Get-PSProvider Import-Module

Log files are great places to look when troubleshooting or analyzing your website traffic. Every time your IIS receives a client’s request, it records that request in a log file. In this post, I want to show you how to use PowerShell to configure and manage IIS log files, such as changing the logging frequency or setting the folder where IIS will store log files or, if you prefer, how to turn off logging.

The following post assumes that you have a Windows Server 2008 R2 or higher with the installed web server role. If you want to know how to do this configuration, maybe my previous post: " How to install IIS on Windows Server with SSL using PowerShell," may interest you.

PowerShell Workaround>

PowerShell Workaround #

Requirements:

  • PowerShell version 3.0 or higher.
  • You must be logged on to a server as an administrator to install or uninstall roles, role services, and features.
Import the WebAdministration module>

Import the WebAdministration module #

To make sure the IIS provider is loaded, import the WebAdministration module using the following command.

Import-Module `
    -Name WebAdministration
Verify the current configurations>

Verify the current configurations #

The above module import also loads the WebAdministration Powershell provider. This provider allows you to browse aspects of the web server, including sites, application pools, and SSL bindings on the host.

Get-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile

IIS logging

Check the current location of IIS Log files.>

Check the current location of IIS Log files. #

Using the following commands, you can get the current location of the Logs files.

$LogfileDirectory = (Get-ItemProperty -Path 'IIS:\Sites\Default Web Site' -Name logfile).directory

$LogfileFolder = [System.Environment]::ExpandEnvironmentVariables("$LogfileDirectory")

Get-ChildItem `
    -Path $LogfileFolder `
    -Recurse

IIS logging
By default, the IIS stores the log file in the %systemdrive%\inetpub\logs\LogFiles.

Set the Log Files New Location>

Set the Log Files New Location #

To change the location of your website’s log files, you should use the following command.

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.directory `
    -Value 'D:\IISLogs'

(Get-ItemProperty -Path 'IIS:\Sites\Default Web Site' -Name logfile).directory

Note that best practices recommend that the log files be stored in a directory other than % systemroot%.

Change the Log Files output format.>

Change the Log Files output format. #

If you want to change the log file format, you should use the following command to modify the logformat attribute. By default, the format is W3C, but you can select the following formats: W3C, IIS, NCSA, and custom.

Get-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.logformat

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.logFormat `
    -Value 'W3C'

If you want to know more about this topic, check out this link.

Log Event Destination>

Log Event Destination #

In IIS 8.5, the administrator can send log information to Event Tracing for Windows (ETW). To do this, you should change the logTargetW3C attribute using the following command. By default, IIS sends the information to the Log file only, but you can use the following options: File, ETW, or both.

Get-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.logTargetW3C

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.logTargetW3C `
    -Value 'File,ETW'
Set Log File Rollover Options>

Set Log File Rollover Options #

Change the frequency of the log file changes.>

Change the frequency of the log file changes. #

You can change the frequency of the log file changes. By default, IIS produces one file per day, but you can set your logfile changes: Hourly, Daily, Weekly, Monthly, or Maximum size.

Get-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.period

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.period `
    -Value weekly
Set a maximum size for the log file>

Set a maximum size for the log file #

To create a log file when the file reaches a certain size, you should use the following command. In the following example, I set the maximum value of the file to 1 Gb expressed in bytes.

Get-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.truncateSize.value

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.truncateSize `
    -Value 1073741824

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.period `
    -Value MaxSize

By default, the minimum file size is 1048576 bytes. If this attribute is set to a value less than 1048576 bytes, the default value is implicitly assumed as 1048576 bytes.

Use the local time instead of UTC for naming and logging>

Use the local time instead of UTC for naming and logging #

Set the value to true for the localTimeRollover attribute if you want to create a new log file based on local time by default, the value is false, and Coordinated Universal Time (UTC) is used

Get-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.localTimeRollover

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name logfile.localTimeRollover `
    -Value $true
Disable IIS Logging>

Disable IIS Logging #

Finally, if you want to disable your website’s Logging, you should use the following command.

Set-ItemProperty `
    -Path 'IIS:\Sites\Default Web Site' `
    -Name Logfile.enabled `
    -Value $false

Thanks for reading my post. I hope you find it useful.

If you want to know more about Internet Information Server (IIS), check out this link.