tail -f /dev/null

If you haven't had any obstacles lately, you're not challenging. be the worst.

IIS: Enhanced LoggingでX-Forwarded-For fieldを追加する

作業メモ。

Environment

  • IIS10
    • 当該featureは8.5 ~

PowerShell webadminstration module

PS> Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/site[@name='<site_name>']/logFile/customFields" -name "." -value @{logFieldName='ContosoField';sourceName='ContosoSource';sourceType='RequestHeader'}

PowerShell appcmd

PS> C:\Windows\System32\inetsrv\appcmd.exe set config  -section:system.applicationHost/sites /+"[name='<site_name>'].logFile.customFields.[logFieldName='X-Forwarded-For',sourceName='X-Forwarded-For',sourceType='RequestHeader']" /commit:apphost
Applied configuration changes to section "system.applicationHost/sites" for "MACHINE/WEBROOT/APPHOST" at configuration commit path "MACHINE/WEBROOT/APPHOST"

Confirmation

PS C:\Users\webdeploy> Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/si
tes/site[@name='<site_name>']/logFile/customFields" -Name 'Collection'


logFieldName   : X-Forwarded-For
sourceName     : X-Forwarded-For
sourceType     : RequestHeader
ItemXPath      : /system.applicationHost/sites/site[@name='<site_name>' and @id='1']/logFile/customFields
Attributes     : {logFieldName, sourceName, sourceType}
ChildElements  : {}
ElementTagName : add
Methods        :
Schema         : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema

logFieldName   : ContosoField
sourceName     : ContosoSource
sourceType     : RequestHeader
ItemXPath      : /system.applicationHost/sites/site[@name='<site_name>' and @id='1']/logFile/customFields
Attributes     : {logFieldName, sourceName, sourceType}
ChildElements  : {}
ElementTagName : add
Methods        :
Schema         : Microsoft.IIs.PowerShell.Framework.ConfigurationElementSchema


PS > $siteLogFileCustom = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -filter "syst
em.applicationHost/sites/site[@name='<site_name>']/logFile/customFields" -Name 'Collection'
PS > $siteLogFileCustom.logFieldName
X-Forwarded-For
ContosoField

Chef recipe

    node[:iis][role_name][:sites].keys.each do |site|
      powershell_script "#{site} Enhanced Logging Setting" do
        code <<-EOS
          $enhanced_logging = Get-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' `
            -filter "system.applicationHost/sites/site[@name='#{site}']/logFile/customFields" -Name 'Collection'

          if ( $enhanced_logging.logFieldName -notmatch "X-Forwarded-For" ) {
              Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' `
                -filter "system.applicationHost/sites/site[@name='#{site}']/logFile/customFields" -name "." `
                  -value @{logFieldName='X-Forwarded-For';sourceName='X-Forwarded-For';sourceType='RequestHeader'}
          }
        EOS
      end
    end

References