HTTP 日志记录 <httpLogging>

概述

<httpLogging> 元素可用于配置 IIS,以便仅为成功的请求和/或失败的请求生成日志条目。 在服务器级别为每个网站配置日志记录后,可以使用此元素为单个 URL 启用选择性日志记录。 默认情况下,将为 Internet Information Services (IIS) 7 上的所有请求启用 HTTP 日志记录。

您可以随时查看站点的日志文件,以了解哪些请求失败,哪些请求成功。 当您不再希望 IIS 记录某个站点的某些请求时,请禁用该站点的日志记录。

兼容性

版本 说明
IIS 10.0 <httpLogging> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <httpLogging> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <httpLogging> 元素在 IIS 8.0 中未进行修改。
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. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 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. 在“操作”窗格中,单击“启用”以启用日志记录。
    Screenshot of the Logging pane. The text Use this feature to configure how I I S logs request server is shown.

  5. 在“格式”下拉列表中选择要用于站点或应用程序的日志文件格式,如果要更改 IIS 存储日志文件的默认位置,请在“目录”框中键入要存储站点或应用程序的日志文件路径。

  6. (可选)如果在步骤 5 的“格式”下拉列表中选择了 W3C,请单击“选择字段”。

  7. (可选)在“W3C 日志记录字段”对话框中,选择要记录的 W3C 字段,清除不想记录的任何 W3C 字段,然后单击“确定”。
    Screenshot of the W three C Logging Fields dialog box. The checkboxes for Date, Time, Client I P Address, User Name, Service Name and Server I P Address are checked. Service Name is highlighted.

  8. 在“操作”窗格中,单击“应用”
    Screenshot of the Actions pane. The One log file box is shown.

配置

使用 ApplicationHost.config 文件,可以在服务器级别配置 <httpLogging> 元素,使用相应的 Web.config 文件,则可以在站点、应用程序或 URL 级别配置。

特性

属性 说明
dontLog 可选布尔属性。

指定是否为成功请求启用 HTTP 日志记录。 如果请求的状态代码小于 400,则被视为成功。

默认值为 false
selectiveLogging 可选枚举特性。

指定要记录的请求类型。

selectiveLogging 属性的值可以是下列其中一个。

默认值为 LogAll
说明
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 配置这些设置时,必须确保将 commit 参数设置为 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()