預設路徑:
%SystemRoot%\System32\LogFiles\W3SVC1\ # WWW 服務 (網站)
%SystemRoot%\System32\LogFiles\W3SVC2\ # 第 2 個網站
...
%SystemRoot%\System32\LogFiles\FTPSVC1\ # FTP 服務
%SystemRoot%\System32\LogFiles\MSFTPSVC1\ # 舊版 FTP
快速開啟:Win + R → %SystemRoot%\System32\LogFiles → Enter
# 命令列批次刪除 7 天前的 .log 檔 (PowerShell 管理員)
Get-ChildItem "C:\Windows\System32\LogFiles\W3SVC*" -Recurse -Filter "*.log" -File | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -Force
# 或 CMD
for /d %i in ("C:\Windows\System32\LogFiles\W3SVC*") do forfiles /p "%i" /m *.log /d -7 /c "cmd /c del @path"
# 圖形介面:依日期排序 → 選取舊檔 → Shift+Delete 永久刪除
⚠️ **鐵律:保留最近 3-7 天日誌**,便於故障排查、安全審計、合規留存。
rd /s /q %systemdrive%\$Recycle.Bin (管理員)dir C:\ /s /a | findstr /c:"bytes free"
# 或
fsutil volume diskfree C:
通常可釋放 **數 GB 至數十 GB** 空間,服務立即恢復正常。
D:\IISLogs\W3SVC1IIS 管理員 → 記錄 → 選擇 每日建立新檔案 → 勾選 使用本地時間命名 → 設定 最大檔案大小 (如 100MB) → 啟用 限制總大小 (如 5GB)。
# PowerShell 腳本:清理 14 天前日誌,保留近 14 天
# 儲存為 C:\Scripts\Clean-IISLogs.ps1
$LogPaths = @(
"C:\Windows\System32\LogFiles\W3SVC*",
"C:\Windows\System32\LogFiles\FTPSVC*",
"D:\IISLogs\W3SVC*",
"D:\IISLogs\FTPSVC*"
)
$DaysToKeep = 14
foreach ($path in $LogPaths) {
Get-ChildItem $path -Recurse -Filter "*.log" -File -ErrorAction SilentlyContinue |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-$DaysToKeep) } |
Remove-Item -Force -ErrorAction SilentlyContinue
Write-Host "Cleaned: $path"
}
建立排程任務:
powershell.exe -ExecutionPolicy Bypass -File C:\Scripts\Clean-IISLogs.ps1IIS → 記錄 → 選取欄位 → **取消勾選** 非必要欄位(如 cs(Cookie), cs(Referer), sc-bytes, cs-bytes 等),僅保留核心欄位:date time s-ip cs-method cs-uri-stem sc-status time-taken。
# applicationHost.config
← 新增此行
| 路徑/項目 | 清理方式 | 風險 |
|---|---|---|
C:\Windows\Temp | 清理舊暫存檔 (服務重啟後安全) | 低 |
C:\Windows\SoftwareDistribution\Download | 停止 wuauserv → 刪除內容 → 重啟服務 | 低 (更新會重下) |
C:\Windows\WinSxS | Dism /online /Cleanup-Image /StartComponentCleanup /ResetBase | 中 (不可回滾更新) |
C:\inetpub\wwwroot\... | 遷移網站內容至資料盤 | 需修改 IIS 物理路徑 |
C:\ProgramData\... | 檢查大型應用快取/日誌 (SQL Server, Exchange 等) | 視應用而定 |
| 頁面檔 / 休眠檔 | 移至資料盤 / 關閉休眠 powercfg /h off | 低 |
# PowerShell 監控腳本,C 槽剩餘 < 2GB 發送郵件/Teams/Slack 警報
$free = (Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'").FreeSpace / 1GB
if ($free -lt 2) {
Send-MailMessage -To "admin@company.com" -Subject "⚠️ C槽空間不足: $([math]::Round($free,1)) GB" -Body "請立即清理 IIS 日誌或擴容"
}
部署為每小時執行的排程任務。
C 槽爆滿 90% 是 IIS 日誌惹的禍。**急救:刪舊日誌 + 清回收站;根治:遷移日誌路徑 + 設定輪替保留 + 自動化清理排程 + 監控預警**。三步走徹底解決,讓伺服器長期穩定運行。