記錄檔 < customFields 中的預設自訂欄位>
概觀
專案的 <customFields>
元素 <logFile>
會指定 W3C 記錄中預設自訂欄位集合的組態設定。
IIS 8.5 可讓您除了標準記錄集之外,還記錄自訂欄位。 這些自訂欄位可以包含來自要求標頭、回應標頭或伺服器變數的資料。 若要記錄這些欄位,您可以直接設定組態屬性,而不是建立自訂記錄模組。 此功能僅適用于網站層級。 記錄檔格式必須是 W3C,才能新增自訂欄位。
將自訂欄位新增至標準集時,「_x」 會附加至檔案名,以顯示記錄檔包含自訂欄位。 自訂欄位中新增的資料總數不能超過 65,536 個位元組。 如果自訂記錄的資料超過該數量,IIS 將會截斷資料。 maxCustomFieldLength 屬性會指定任何一個自訂欄位中可新增至記錄檔的資料量上限。
若要設定自訂欄位,請指定功能變數名稱、來源名稱和來源類型。 您可以將自訂資訊放入伺服器變數中,並記錄伺服器變數。 選取來源類型之後,您可以選取現有的來源名稱或輸入新的來源名稱。
自訂欄位可讓您收集有關進程的實用資料,並將其匯總至 IIS 記錄。 在包含負載平衡器的系統中,您可能會在記錄中看到負載平衡器的 IP 位址,但您可以在自訂欄位中記錄 X-Forwarded-For 標頭,以便知道原始要求者。 您可以記錄進程執行時間,以查看進程在一天中重新開機的次數。 如果記憶體過度使用,您可以判斷它開始耗用記憶體的時間、要求的頁面,以及用戶端的識別碼 (,如果用戶端執行惡意) ,則特別有用。
相容性
版本 | 備註 |
---|---|
IIS 10.0 | 未在 IIS 10.0 中修改專案 <customFields> 。 |
IIS 8.5 | 元素 <customFields> 是在 IIS 8.5 中引進。 |
IIS 8.0 | N/A |
IIS 7.5 | N/A |
IIS 7.0 | N/A |
IIS 6.0 | N/A |
安裝程式
元素 <customFields>
包含在 IIS 8.5 和更新版本的預設安裝中。
作法
如何新增預設自訂欄位
開啟 [Internet Information Services (IIS) 管理員:
如果您使用 Windows Server 2012 R2:
- 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
如果您使用 Windows 8.1:
- 按住Windows鍵,按字母X,然後按一下[主控台]。
- 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
在 [ 連線] 窗格中,選取伺服器。
按兩下IIS區段中的[記錄]。
在 [ 記錄 ] 首頁的 [ 格式] 中,選取 [W3C]。
按一下 [選取欄位]。
在 [ W3C 記錄欄位 ] 對話方塊中,按一下 [ 新增欄位]。
在 [ 新增自訂欄位 ] 對話方塊中,在 [功能變數名稱 ] 中輸入名稱, (不含空格) ,然後針對 [來源類型]:[ 要求標頭]、[ 回應標頭] 或 [ 伺服器變數] 選取下列其中一項。
在 [來源] 中,從清單中選取來源,或輸入自訂來源的名稱。
按一下 [確定],然後按一下 [ 確定 ]。
在 [ 動作 ] 窗格中,按一下 [ 套用]。
如何設定預設最大自訂欄位長度
開啟 [Internet Information Services (IIS) 管理員:
如果您使用 Windows Server 2012 R2:
- 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
如果您使用 Windows 8.1:
- 按住Windows鍵,按字母X,然後按一下[主控台]。
- 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
在 [連線]窗格中,選取伺服器,然後在 [管理] 區域中,按兩下 [組態編輯器]。
在 [ 組態編輯器]的 區段中,選取 system.applicationHost,然後選取 [月臺]。
按一下 [siteDefaults]。
選取網站、展開 logFile、 customFields,然後按一下 maxCustomFieldLength。
針對 maxCustomFieldLength,輸入任何一個自訂欄位中可新增至記錄檔的最大資料量,以位元組為單位。
關閉 [集合編輯器],然後在 [ 動作 ] 窗格中,按一下 [ 套用]。
組態
預設 <customFields>
設定的 元素是在伺服器層級設定。
屬性
屬性 | 描述 |
---|---|
maxCustomFieldLength |
選擇性 uint 屬性。 可以新增至任何一個自訂欄位中記錄檔的最大資料量,以位元組為單位。 範圍是 2 到 65,536。 預設值是 4096 。 |
子元素
元素 | 描述 |
---|---|
add |
選擇性項目。 指定 W3C 記錄檔中預設自訂欄位的組態設定。 |
組態範例
下列組態範例會 customFields
使用 元素及其 add
子項目來指定 W3C 記錄的預設自訂欄位設定。
<siteDefaults>
<logFile logFormat="W3C" directory="%SystemDrive%\inetpub\logs\LogFiles" flushByEntryCountW3CLog="0">
<customFields>
<clear />
<add logFieldName="ContosoField" sourceName="ContosoSource" sourceType="ServerVariable" />
</customFields>
</logFile>
</siteDefaults>
範例程式碼
下列範例會設定 W3C 記錄的預設自訂欄位。
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites /+"siteDefaults.logFile.customFields.[logFieldName='ContosoField',sourceName='ContosoSource',sourceType='ServerVariable']" /commit:apphost
注意
當您使用 AppCmd.exe 來設定這些設定時,請務必將 認可 參數 apphost
設定為 。 這會將組態設定認可至ApplicationHost.config檔案中的適當位置區段。
C#
using System;
using System.Text;
using Microsoft.Web.Administration;
internal static class Sample
{
private static void Main()
{
using(ServerManager serverManager = new ServerManager())
{
Configuration config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
ConfigurationElement siteDefaultsElement = sitesSection.GetChildElement("siteDefaults");
ConfigurationElement logFileElement = siteDefaultsElement.GetChildElement("logFile");
ConfigurationElement customFieldsElement = logFileElement.GetChildElement("customFields");
ConfigurationElementCollection customFieldsCollection = customFieldsElement.GetCollection();
ConfigurationElement addElement = customFieldsCollection.CreateElement("add");
addElement["logFieldName"] = @"ContosoField";
addElement["sourceName"] = @"ContosoSource";
addElement["sourceType"] = @"ServerVariable";
customFieldsCollection.Add(addElement);
serverManager.CommitChanges();
}
}
}
VB.NET
Imports System
Imports System.Text
Imports Microsoft.Web.Administration
Module Sample
Sub Main()
Dim serverManager As ServerManager = New ServerManager
Dim config As Configuration = serverManager.GetApplicationHostConfiguration
Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
Dim siteDefaultsElement As ConfigurationElement = sitesSection.GetChildElement("siteDefaults")
Dim logFileElement As ConfigurationElement = siteDefaultsElement.GetChildElement("logFile")
Dim customFieldsElement As ConfigurationElement = logFileElement.GetChildElement("customFields")
Dim customFieldsCollection As ConfigurationElementCollection = customFieldsElement.GetCollection
Dim addElement As ConfigurationElement = customFieldsCollection.CreateElement("add")
addElement("logFieldName") = "ContosoField"
addElement("sourceName") = "ContosoSource"
addElement("sourceType") = "ServerVariable"
customFieldsCollection.Add(addElement)
serverManager.CommitChanges
End Sub
End Module
JavaScript
var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var siteDefaultsElement = sitesSection.ChildElements.Item("siteDefaults");
var logFileElement = siteDefaultsElement.ChildElements.Item("logFile");
var customFieldsElement = logFileElement.ChildElements.Item("customFields");
var customFieldsCollection = customFieldsElement.Collection;
var addElement = customFieldsCollection.CreateNewElement("add");
addElement.Properties.Item("logFieldName").Value = "ContosoField";
addElement.Properties.Item("sourceName").Value = "ContosoSource";
addElement.Properties.Item("sourceType").Value = "ServerVariable";
customFieldsCollection.AddElement(addElement);
adminManager.CommitChanges();
VBScript
Set adminManager = CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set siteDefaultsElement = sitesSection.ChildElements.Item("siteDefaults")
Set logFileElement = siteDefaultsElement.ChildElements.Item("logFile")
Set customFieldsElement = logFileElement.ChildElements.Item("customFields")
Set customFieldsCollection = customFieldsElement.Collection
Set addElement = customFieldsCollection.CreateNewElement("add")
addElement.Properties.Item("logFieldName").Value = "ContosoField"
addElement.Properties.Item("sourceName").Value = "ContosoSource"
addElement.Properties.Item("sourceType").Value = "ServerVariable"
customFieldsCollection.AddElement(addElement)
adminManager.CommitChanges()
PowerShell
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/siteDefaults/logFile/customFields" -name "." -value @{logFieldName='ContosoField';sourceName='ContosoSource';sourceType='ServerVariable'}