IIS (Internet Information Services) · Operations

Logging

IIS logs every request: who asked for what, the status code and how long it took. They are your first stop for traffic and error analysis.

Advanced 5 min Lesson 27 of 33Covers IIS 10 on Windows Server 2019/2022/2025 and Windows 10/11.
Before you start:IIS Architecture
0/33 in IIS
On this page · 5 min · Advanced

In simple words#

IIS logs are the visitor register at the gate: time of entry, visitor’s address, which room they asked for, whether they were let in, and how long they stayed.

W3C format
Space-separated text log with chosen fields.
time-taken
How long the request took (ms).
sc-status
HTTP status code returned.

Learning objectives#

  • Find and read IIS logs
  • Choose useful fields
  • Log the real client IP behind proxies
  • Analyse logs with PowerShell

Explanation#

Default location: %SystemDrive%\inetpub\logs\LogFiles\W3SVC<siteId>\u_exYYMMDD.log, one file per day.

Add useful fields: time-taken, cs-host, sc-substatus, sc-win32-status, cs(User-Agent), cs(Referer).

Behind a load balancer, add a custom field for X-Forwarded-For to capture the real client IP.

Logs are written with a short delay (buffered). Rotate and archive them; they can fill disks.

Code example: Find slow and failing requests#

PowerShellFind slow and failing requests Illustrative
$log = Get-ChildItem "C:\inetpub\logs\LogFiles\W3SVC1\*.log" | Sort-Object LastWriteTime | Select-Object -Last 1
$fields = (Get-Content $log.FullName -TotalCount 5 | Where-Object { $_ -like '#Fields:*' }) -replace '#Fields: ' -split ' '


$rows = Get-Content $log.FullName | Where-Object { $_ -notlike '#*' } | ConvertFrom-Csv -Delimiter ' ' -Header $fields


$rows | Where-Object { [int]$_.'sc-status' -ge 500 } | Group-Object 'cs-uri-stem' | Sort-Object Count -Desc | Select-Object -First 5 Count, Name
$rows | Sort-Object { [int]$_.'time-taken' } -Descending | Select-Object -First 5 'cs-uri-stem', 'time-taken', 'sc-status'


# Add X-Forwarded-For as a custom field (site level)
# Add-WebConfigurationProperty -PSPath IIS:\ -Filter "system.applicationHost/sites/site[@name='Shop']/logFile/customFields" -Name "." `
#   -Value @{ logFieldName='X-Forwarded-For'; sourceName='X-Forwarded-For'; sourceType='RequestHeader' }
Expected output
Count Name
----- ----
   42 /api/orders
    3 /checkout

cs-uri-stem      time-taken sc-status
-----------      ---------- ---------
/api/reports         8734       200
/api/orders          5120       500

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. The latest log file for site 1 is read.
  2. The #Fields header provides column names.
  3. Errors are grouped by URL; slow requests are sorted by time-taken.
  4. A custom field can log the real client IP.

Real-world use case#

After a release, logs show 42 errors on /api/orders, pointing the team straight to the broken endpoint.

Common mistakes#

Logs filling the C: drive

Move logs to another drive and archive or delete old files on a schedule.

Only the load balancer IP in logs

Add an X-Forwarded-For custom field.

Best practices#

  • Add time-taken and sub-status fields
  • Log the real client IP
  • Rotate and archive logs
  • Ship logs to a central tool

Remember this#

Logs: inetpub\logs\LogFiles\W3SVC<id>.

sc-status + sc-substatus + time-taken.

Behind proxies log X-Forwarded-For.

Practice exercise#

Your task: Move the Shop site’s logs to D:\iislogs.

Show solution
PowerShell Illustrative
Set-ItemProperty "IIS:\Sites\Shop" -Name logFile.directory -Value "D:\iislogs"

Mini quiz#

Logging quizMultiple choice Question 1 of 1

Which log field shows how long a request took?

Score so far: 0/0

Want more? Practise all IIS MCQs · IIS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.