HTTP 記錄 <HTTPLogging>

概觀

元素 <httpLogging> 可讓您設定 IIS 只針對成功的要求、失敗的要求或兩者產生記錄專案。 在伺服器層級設定每個網站的記錄之後,您可以使用這個項目來啟用個別 URL 的選擇性記錄。 根據預設,INTERNET Information Services 上的所有要求都會啟用 HTTP 記錄, (IIS) 7。

您可以隨時檢視月臺的記錄檔,以查看哪些要求失敗,以及哪些要求成功。 當您不想再讓 IIS 記錄月臺的特定要求時,請停用該網站的記錄。

相容性

版本 備註
IIS 10.0 在 <httpLogging> IIS 10.0 中未修改專案。
IIS 8.5 未 <httpLogging> 在 IIS 8.5 中修改專案。
IIS 8.0 在 IIS 8.0 中未修改專案 <httpLogging> 。
IIS 7.5 未 <httpLogging> 在 IIS 7.5 中修改專案。
IIS 7.0 專案 <httpLogging> 是在 IIS 7.0 中引進的。
IIS 6.0 元素 <httpLogging> 和 <logFile> 元素會取代 IIS 6.0 IIsWebService 中繼基底物件上的記錄屬性。

設定

專案 <httpLogging> 包含在 IIS 7 的預設安裝中。

作法

如何啟用網站或應用程式的 HTTP 記錄

  1. (IIS) 管理員開啟 Internet Information Services:

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任務欄上,按兩下 [伺服器管理員]、[工具],然後按兩下 [Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住 Windows 鍵,按字母 X,然後按兩下 控制台。
      • 單擊 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任務欄上,按兩下 [ 開始],指向 [ 系統管理工具],然後按兩下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在任務欄上,按兩下 [開始],然後按兩下 [控制台]。
      • 按兩下 [ 系統管理工具],然後按兩下 [Internet Information Services] ([IIS) 管理員]。
  2. 在 [ 連線 ] 窗格中,展開伺服器名稱、[ 網站],然後流覽至您要設定 HTTP 記錄的網站或應用程式。

  3. 在 [ 首頁] 窗格中,按兩下 [ 記錄]。

  4. 在 [ 動作] 窗格中,按兩下 [ 啟用 ] 以啟用記錄。
    [記錄] 窗格的螢幕快照。文字 使用這項功能來設定如何顯示 I S 記錄要求伺服器。

  5. 在 [ 格式 ] 下拉式清單中選取您要用於網站或應用程式的記錄檔格式,如果您想要變更 IIS 儲存記錄檔的預設位置,請在 [ 目錄 ] 方塊中輸入您要儲存月臺或應用程式的記錄檔路徑。

  6. (選擇性) 如果您在步驟 5 的 [格式] 下拉式清單中選取了 W3C,請按兩下 [選取字段]。

  7. (選擇性) 在 [W3C 記錄欄位 ] 對話框中,選取您要記錄的 W3C 欄位、清除您不想要記錄的任何 W3C 欄位,然後按兩下 [ 確定]。
    [W 三個 C 記錄欄位] 對話框的螢幕快照。已核取 [日期]、[時間]、[用戶端 I 位址]、[使用者名稱]、[服務名稱] 和 [伺服器 I P 位址] 的複選框。服務名稱會反白顯示。

  8. 在 [動作] 窗格中,按一下 [套用]。
    [動作] 窗格的螢幕快照。隨即顯示 [一個記錄檔] 方塊。

設定

您可以在 <httpLogging> ApplicationHost.config 檔案和適當 Web.config 檔案的月臺、應用程式或 URL 層級,設定伺服器層級的專案。

屬性

屬性 Description
dontLog 選擇性的 Boolean 屬性。

指定是否針對成功的要求啟用 HTTP 記錄。 如果要求的狀態代碼小於 400,則要求會被視為成功。

預設值是 false。
selectiveLogging 選擇性列舉屬性。

指定要記錄的要求類型。

selectiveLogging 屬性可以是下列其中一個可能的值。

預設值是 LogAll。
值 Description
LogAll 記錄所有要求。

數值為 0。
LogSuccessful 只記錄成功的要求。 成功要求的 HTTP 狀態代碼範圍是 100-399。

數值為 1。
LogError 只記錄不成功的要求。 失敗要求的 HTTP 狀態代碼範圍是 400-999。

數值為 2。

子元素

無。

組態範例

下列組態範例會在月臺或應用程式的 Web.config 檔案中包含時,設定 HTTP 記錄,並指定 IIS 應該只記錄產生錯誤的要求。

<configuration>
   <system.webServer>
      <httpLogging dontLog="false" selectiveLogging="LogError" />
   </system.webServer>
<configuration>

範例程式碼

下列範例會針對名為 Contoso 的網站啟用 HTTP 記錄,並指定 IIS 不應該記錄任何要求。

AppCmd.exe

appcmd.exe set config "Contoso" -section:system.webServer/httpLogging /dontLog:"True" /commit:apphost

appcmd.exe set config "Contoso" -section:system.webServer/httpLogging /selectiveLogging:"LogAll" /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 httpLoggingSection = config.GetSection("system.webServer/httpLogging", "Contoso");
         httpLoggingSection["selectiveLogging"] = @"LogAll";
         httpLoggingSection["dontLog"] = true;
         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 httpLoggingSection As ConfigurationSection = config.GetSection("system.webServer/httpLogging", "Contoso")
      httpLoggingSection("selectiveLogging") = "LogAll"
      httpLoggingSection("dontLog") = True
      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var httpLoggingSection = adminManager.GetAdminSection("system.webServer/httpLogging", "MACHINE/WEBROOT/APPHOST/Contoso");
httpLoggingSection.Properties.Item("selectiveLogging").Value = "LogAll";
httpLoggingSection.Properties.Item("dontLog").Value = true;

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"

Set httpLoggingSection = adminManager.GetAdminSection("system.webServer/httpLogging", "MACHINE/WEBROOT/APPHOST/Contoso")
httpLoggingSection.Properties.Item("selectiveLogging").Value = "LogAll"
httpLoggingSection.Properties.Item("dontLog").Value = True

adminManager.CommitChanges()