天天看點

【Azure 雲服務】Azure Cloud Service 為 Web Role(IIS Host)增加自定義字段

問題描述

把Web Role服務釋出到Azure Cloud Service後,需要在IIS的輸出日志中,把每一個請求的HTTP Request Header中的User-Agent内容也輸出到日志中。通過Cloud Service(雲服務)如何實作呢?

解決方案

方案一(臨時):Cloud Service WebRole是Host在IIS中,是以可以通過對IIS的Logging設定就可以(RDP到VM中修改)

1:RDP到雲服務的執行個體上,搜尋IIS Manager 

2:打開之後,找到雲服務執行個體,點選Logging。 如下截圖所示:

【Azure 雲服務】Azure Cloud Service 為 Web Role(IIS Host)增加自定義字段

3:打開之後,在Log File 裡面的Format 選擇W3C ,點選Select Fields,進行添加自定義字段

【Azure 雲服務】Azure Cloud Service 為 Web Role(IIS Host)增加自定義字段

4:修改完成後,新生成的IIS日志中就會包含列名為UUID,内容為User-Agent的内容。

【Azure 雲服務】Azure Cloud Service 為 Web Role(IIS Host)增加自定義字段

方案二:通過Cloud Service的啟動任務對IIS Logging進行設定(需重新部署一次雲服務)

1:在Cloud Service中設定啟動任務,在WebRole項目的ServiceDefinition.csdef檔案中加入 Startup 内容:

<Startup>
    <Task commandLine="Startup.cmd" executionContext="limited" taskType="simple" >
        <Environment>
            <Variable name="MyVersionNumber" value="1.0.0.0"      

2: 在項目根目錄中添加檔案 Startup.cmd,檔案内容為:

d /d "%~dp0"

%windir%\system32\inetsrv\appcmd.exe set config -section:system.applicationHost/sites /+"siteDefaults.logFile.customFields.[logFieldName='UUID',sourceName='User-Agent',sourceType='RequestHeader']" /commit:apphost >> "%TEMP%\StartupLog.txt" 2>&1

exit /b 0      

PS: 通過修改IIS config的方式來配置Loggin中的自定義日志字段

3: 重新部署整個Cloud Service,即可生效。

參考資料

如何配置和運作 Azure 雲服務(經典)的啟動任務:​​https://docs.microsoft.com/zh-cn/azure/cloud-services/cloud-services-startup-tasks#example-of-a-startup-task​​

Adding Custom Fields to a Log File for a Site <add> : ​​https://docs.microsoft.com/en-us/iis/configuration/system.applicationhost/sites/site/logfile/customfields/add#sample-code​​

如何在部署雲服務Cloud Service時候通過啟動任務Start Task來配置IIS (如開啟ARR)

繼續閱讀