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.
On this page · 5 min · Advanced
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Oct 30, 2026
Related tutorials
Interview questions
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#
$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' }
Count Name
----- ----
42 /api/orders
3 /checkout
cs-uri-stem time-taken sc-status
----------- ---------- ---------
/api/reports 8734 200
/api/orders 5120 500This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The latest log file for site 1 is read.
- The #Fields header provides column names.
- Errors are grouped by URL; slow requests are sorted by time-taken.
- 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#
Move logs to another drive and archive or delete old files on a schedule.
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
Set-ItemProperty "IIS:\Sites\Shop" -Name logFile.directory -Value "D:\iislogs"
Mini quiz#
Which log field shows how long a request took?
Want more? Practise all IIS MCQs · IIS interview questions
My notes#
Saved in this browser.