使用 Add-WebConfigurationProperty 向 IIS 添加自定义日志记录字段不起作用

使用 Add-WebConfigurationProperty 向 IIS 添加自定义日志记录字段不起作用

我真的很难理解这一点。该命令类似于已确认的这里以及许多其他网站。

我想通过Add-WebConfigurationPropertyPowershell 命令向 IIS 添加自定义日志记录字段。自定义字段名称是X-Forwarded-For。但是,我不得不尝试添加一个test字段。我在 Windows Server 2019 上使用 IIS v10。

我用的是这个:

Add-WebConfigurationProperty -filter "system.applicationHost/sites/siteDefaults/logFile/customFields" -name "." -value @{logFieldName='test';sourceName='X-Forwarded-For';sourceType='RequestHeader'}

我第一次运行它时得到以下信息:

Add-WebConfigurationProperty : Filename: \\?\C:\Windows\system32\inetsrv\config\applicationHost.config
Error: Cannot commit configuration changes because the file has changed on disk

第二次运行它时,我得到了这个:

Add-WebConfigurationProperty : Filename: 
Error: Cannot add duplicate collection entry of type 'add' with unique key attribute 'logFieldName' set to 'test'

并且,它从来没有修改过 applicationHost.config 文件:

    <siteDefaults>
        <logFile logExtFileFlags="Date, Time, ClientIP, UserName, ServerIP, Method, UriStem, UriQuery, HttpStatus, Win32Status, BytesSent, BytesRecv, TimeTaken, ServerPort, UserAgent, Referer, HttpSubStatus" logFormat="W3C" directory="D:\inetpub\logs\logfiles">
            <customFields>
                <clear />
            </customFields>
        </logFile>
        <traceFailedRequestsLogging directory="D:\inetpub\logs" />
        <ftpServer>
            <logFile directory="D:\inetpub\logs\logfiles" />
        </ftpServer>
    </siteDefaults>

该字段也从未出现在 IIS 管理器中。它也不会显示Get-WebConfigurationProperty

此外,我X-Forwarded-For通过 IIS 管理器 GUI 手动添加了(它立即正确修改了 applicationHost.config 并通过 显示Get-WebConfigurationProperty),然后尝试通过 Powershell 添加它,但再次出现无法添加“重复”字段的错误。然后,当我通过 GUI 删除该字段并再次运行该命令时,我收到无法提交更改的错误。如果我再次运行该命令,我会收到无法添加重复的错误。但是,applicationHost.config 仍然没有被修改。因此,很明显该命令确实影响了我在 GUI 中更改的同一区域,并且它似乎做了一些事情,因为我无法添加重复项,但更改从未出现在 applicationHost.config 或 GUI 中。我尝试关闭并重新打开 IIS 管理器并重新启动 IIS。

我错过了什么?

答案1

显然,我的 Powershell 会话搞乱了。

关闭 Powershell 并重新打开后,我能够成功执行命令,并且刷新后更改立即显示在 GUI 和 applicationHost.config 中。

在以下情况下,问题会再次出现:

  1. 我尝试添加“重复”属性
  2. 我从 IIS GUI 中手动删除了该属性
  3. 我尝试再次添加该属性。然后出现提交失败错误。

此后,必须关闭 Powershell 并重新打开,然后我才能成功添加该属性。

答案2

相关内容