Skip to content

Instantly share code, notes, and snippets.

@chenbojian
Last active September 29, 2020 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chenbojian/5717b21b4e20253d05a7daaf9032b14a to your computer and use it in GitHub Desktop.
Save chenbojian/5717b21b4e20253d05a7daaf9032b14a to your computer and use it in GitHub Desktop.
# 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