Last active
September 29, 2020 09:10
-
-
Save chenbojian/5717b21b4e20253d05a7daaf9032b14a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ref: https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/logfile/ | |
# ref: https://forums.iis.net/t/1205491.aspx?Add+IIS+8+5+Custom+Logging+Fields+with+Powershell | |
function Set-WebSiteIISLogFormat { | |
param ( | |
$Site | |
) | |
$logExtFileFlags = 'Date,Time,ClientIP,UserName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,TimeTaken,ServerPort,UserAgent,Referer,Host,HttpSubStatus' | |
Set-ItemProperty -Path "IIS:\Sites\$Site" -Name logfile.logExtFileFlags -Value $logExtFileFlags | |
$customFields = Get-ItemProperty -Path "IIS:\Sites\$Site" -Name logfile.customFields.collection | |
if (-not ($customFields | Where-Object logFieldName -eq X-Forwarded-For)) { | |
New-ItemProperty -Path "IIS:\Sites\$Site" -Name logfile.customFields.collection -Value @{ | |
logFieldName = 'X-Forwarded-For' | |
sourceName = 'X-Forwarded-For' | |
sourceType = 'RequestHeader' | |
} | |
} | |
} | |
function Set-DefaultIISLogFormat { | |
$logExtFileFlags = 'Date,Time,ClientIP,UserName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,TimeTaken,ServerPort,UserAgent,Referer,Host,HttpSubStatus' | |
Set-ItemProperty -Path IIS: -Name siteDefaults.logfile.logExtFileFlags -Value $logExtFileFlags | |
$customFields = Get-ItemProperty -Path IIS: -Name siteDefaults.logfile.customFields.collection | |
if (-not ($customFields | Where-Object logFieldName -eq X-Forwarded-For)) { | |
New-ItemProperty -Path IIS: -Name siteDefaults.logfile.customFields.collection -Value @{ | |
logFieldName = 'X-Forwarded-For' | |
sourceName = 'X-Forwarded-For' | |
sourceType = 'RequestHeader' | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment