添加跟踪失败的请求 <add>

概述

<add> 集合中的每个 <traceFailedRequests> 元素都可以包含一个 <traceAreas> 元素,该元素定义跟踪提供程序和提供程序特定的区域,这些区域将为当前跟踪规则生成跟踪事件。 还可以包含一个 <failureDefinition> 元素,可用于配置认为请求失败的条件,包括服务器响应所需的时间、HTTP 状态代码和跟踪中包含的信息量。

兼容性

版本 说明
IIS 10.0 <add> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <add> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 <add> 元素在 IIS 8.0 中未进行修改。
IIS 7.5 <add> 元素在 IIS 7.5 中未进行修改。
IIS 7.0 IIS 7.0 中引入了 <traceFailedRequests> 集合的 <add> 元素。
IIS 6.0 空值

安装

完成 IIS 7 及更高版本的默认安装后,必须安装跟踪角色服务才能使用失败请求跟踪。 安装角色服务后,仍必须在站点级别、应用程序级别或目录级别启用失败请求跟踪。

Windows Server 2012 或 Windows Server 2012 R2

  1. 在任务栏上,单击 “服务器管理器”。
  2. 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
  3. 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
  4. 在“服务器角色”页上,依次展开“Web 服务器 (IIS)”、“Web 服务器”和“运行状况和诊断”,然后选择“跟踪”。 单击 “下一步”
    Screenshot of Server Roles page, with the Health and Diagnostics tree expanded and Tracing selected and outlined.
  5. 在“选择功能”页上,单击“下一步”
  6. “确认安装选择”页上,单击“安装”
  7. 在“结果” 页面中单击“关闭”

Windows 8 或 Windows 8.1

  1. 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 依次展开“Internet Information Services”、“万维网服务”、“运行状况和诊断”,然后选择“跟踪”
    Screenshot of Internet World Wide Web tree in Control Panel, with Health and Diagnostics expanded and Tracing selected.- 单击“确定”。
  4. 单击“关闭” 。

Windows Server 2008 或 Windows Server 2008 R2

  1. 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
  2. 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器 (IIS)”。
  3. 在“Web 服务器 (IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
  4. 在“添加角色服务向导”的“选择角色服务”页上,选择“跟踪”,然后单击“下一步”。
    Screenshot of Select Role Services page of the Add Role Services Wizard, with Health and Diagnostics expanded and Tracing selected.
  5. “确认安装选择”页中,单击“安装”
  6. 在“结果” 页面中单击“关闭”

Windows Vista 或 Windows 7

  1. 在任务栏上,单击“开始”,然后单击“控制面板”。
  2. 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
  3. 扩展“Internet Information Services”,展开“万维网服务”,然后展开“运行状况和诊断”。
  4. 选择“跟踪”,然后单击“确定”。
    Screenshot of World Wide Web Services tree, with Health and Diagnostics expanded and Tracing selected and outlined.

操作方式

如何启用跟踪

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,选择要为其配置失败请求跟踪的服务器连接、站点、应用程序或目录。

  3. 在“操作”窗格中,单击“失败请求跟踪...
    Screenshot of Internet Information Services Manager dialog, with Failed Request Tracing selected in Actions pane.

  4. 在“编辑网站失败请求跟踪设置”对话框中,选中“启用”复选框以启用跟踪,保留默认值或在“目录”框中键入要存储失败请求日志文件的新目录,键入要在“最大跟踪文件数”框中存储的失败请求跟踪文件数,然后单击“确定”
    Screenshot of Edit Web Site Failed Request Tracing Settings dialog box, with Enable check box selected to enable tracing.

如何配置失败定义

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,转到要为其配置失败请求跟踪的连接、站点、应用程序或目录。

  3. 在“开始”窗格中,双击“失败请求跟踪规则”。
    Screenshot of Internet Information Services Manager dialog, with Failed Request Tracing Rules selected in Home pane.

  4. 在“操作”窗格中,单击“添加...”。

  5. 在“添加失败请求跟踪规则”向导的“指定要跟踪的内容”页上,选择要跟踪的内容类型,然后单击“下一步”。
    Screenshot of Specify Content to Trace page of the Add Failed Request Tracing Rule Wizard, with content type selected.

  6. 在“定义跟踪条件”页上,选择要跟踪的条件,然后单击“下一页”。 跟踪条件可以包括状态代码、请求应采用的时间限制或事件严重性的任意组合。 如果指定所有条件,则满足的第一个条件将生成失败的请求跟踪日志文件。
    Screenshot of Define Trace Conditions page of Add Failed Request Tracing Rule dialog, with conditions selected.

  7. 在“选择跟踪提供程序”页上,选择“提供程序”下的一个或多个跟踪提供程序。
    Screenshot of Select Trace Providers page of Add Failed Request Tracing Rule dialog, with Providers selected.

  8. 在“选择跟踪提供程序”页上,选择“详细程度”下的一个或多个详细级别。
    Screenshot of Select Trace Providers page of Add Failed Request Tracing Rule dialog, with Verbosity levels selected.

  9. 如果在步骤 8 中选择了“ASPNET”或“WWW 服务器”跟踪提供程序,请在“选择跟踪提供程序”页的“区域”下选择提供程序要跟踪的一个或多个功能区域。

  10. 单击“完成”。

配置

特性

属性 说明
customActionExe 可选的字符串属性。

指定达到失败条件时要运行的可执行文件(例如,若要运行 VBScript 文件,请指定“Cscript.exe”)。 将扩展所有命令行变量。 例如,“%windir%”将扩展为 Windows 目录的路径。
customActionParams 可选的字符串属性。

包含要传递给 customActionExe 属性中指定的可执行文件的参数。 将扩展“%windir%”之类的命令行变量。 还可以在指定的字符串中使用“%1”和“%2”之类的参数变量(例如“MyScript.vbs %1 %2”)。
customActionTriggerLimit 可选 uint 属性。

指定由 customActionExe 属性指定的程序在失败请求定义的工作进程生存内执行的最大次数。 每次工作进程回收时,该值都会重置。 允许的值为 0 到 10000。 例如,此功能可用于在下一次失败后创建内存转储,但不能在发生后续失败后创建内存转储。

注意:如果在工作进程运行时达到指定的 CustomActionTriggerLimit 值,则会记录后续失败的请求,但 CustomActionExe 中指定的程序将不会运行。 失败请求日志中的条目将指示已达到 CustomActionTriggerLimit 值。 默认为 1
path 必需的字符串属性。

指定要记录跟踪事件的路径。 路径相对于 URL(虚拟目录/目录)。 不能使用子路径。 此外,路径必须是设置定义的目录的本地路径。 可以使用通配符值,例如“*.aspx”。 若要跟踪默认文档,请使用“/”作为路径值。

子元素

元素 说明
traceAreas 必需的元素。

配置要对给定路径跟踪的内容。
failureDefinitions 必需的元素。

定义用于保存请求跟踪的条件。

配置示例

以下配置示例展示了如何在 ApplicationHost.config 文件中的服务器级别配置跟踪。 它为所有 .aspx 文件设置跟踪,使用 <traceAreas> 元素设置“ASPNET”提供程序,并针包括“基础结构”、“模块”、“页面”和“AppServices”在内的所有 ASP.NET 区域进行跟踪。 此示例还使用“详细程度”属性将返回到跟踪文件的信息量设置为“警告”。 最后,该示例使用 <failureDefinitions> 元素仅跟踪生成 HTTP 404 状态代码的请求。

<tracing>
   <traceFailedRequests>
      <add path="*.aspx">
         <traceAreas>
            <add provider="ASPNET" areas="Infrastructure,Module,Page,AppServices" verbosity="Warning" />
         </traceAreas>
         <failureDefinitions statusCodes="404" />
      </add>
   </traceFailedRequests>
</tracing>

代码示例

以下示例为所有 *.aspx 页面请求的 ASP.NET 内容 HTTP 500 错误启用详细失败请求跟踪。

AppCmd.exe

appcmd.exe set config "Contoso" -section:system.webServer/tracing/traceFailedRequests /+"[path='*.aspx']" 

appcmd.exe set config "Contoso" -section:system.webServer/tracing/traceFailedRequests /+"[path='*.aspx'].traceAreas.[provider='ASPNET',areas='Infrastructure,Module,Page,AppServices',verbosity='Verbose']" 

appcmd.exe set config "Contoso" -section:system.webServer/tracing/traceFailedRequests /[path='*.aspx'].failureDefinitions.statusCodes:"500"

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.GetWebConfiguration("Contoso");
         ConfigurationSection traceFailedRequestsSection = config.GetSection("system.webServer/tracing/traceFailedRequests");
         ConfigurationElementCollection traceFailedRequestsCollection = traceFailedRequestsSection.GetCollection();

         ConfigurationElement addElement = traceFailedRequestsCollection.CreateElement("add");
         addElement["path"] = @"*.aspx";

         ConfigurationElementCollection traceAreasCollection = addElement.GetCollection("traceAreas");
         ConfigurationElement addElement1 = traceAreasCollection.CreateElement("add");
         addElement1["provider"] = @"ASPNET";
         addElement1["areas"] = @"Infrastructure,Module,Page,AppServices";
         addElement1["verbosity"] = @"Verbose";
         traceAreasCollection.Add(addElement1);

         ConfigurationElement failureDefinitionsElement = addElement.GetChildElement("failureDefinitions");
         failureDefinitionsElement["statusCodes"] = @"500";
         traceFailedRequestsCollection.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.GetWebConfiguration("Contoso")
      Dim traceFailedRequestsSection As ConfigurationSection = config.GetSection("system.webServer/tracing/traceFailedRequests")
      Dim traceFailedRequestsCollection As ConfigurationElementCollection = traceFailedRequestsSection.GetCollection

      Dim addElement As ConfigurationElement = traceFailedRequestsCollection.CreateElement("add")
      addElement("path") = "*.aspx"
      Dim traceAreasCollection As ConfigurationElementCollection = addElement.GetCollection("traceAreas")
      Dim addElement1 As ConfigurationElement = traceAreasCollection.CreateElement("add")
      addElement1("provider") = "ASPNET"
      addElement1("areas") = "Infrastructure,Module,Page,AppServices"
      addElement1("verbosity") = "Verbose"
      traceAreasCollection.Add(addElement1)

      Dim failureDefinitionsElement As ConfigurationElement = addElement.GetChildElement("failureDefinitions")
      failureDefinitionsElement("statusCodes") = "500"
      traceFailedRequestsCollection.Add(addElement)

      serverManager.CommitChanges()
   End Sub
End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso";
var traceFailedRequestsSection = adminManager.GetAdminSection("system.webServer/tracing/traceFailedRequests", "MACHINE/WEBROOT/APPHOST/Contoso");
var traceFailedRequestsCollection = traceFailedRequestsSection.Collection;

var addElement = traceFailedRequestsCollection.CreateNewElement("add");
addElement.Properties.Item("path").Value = "*.aspx";
var traceAreasCollection = addElement.ChildElements.Item("traceAreas").Collection;

var addElement1 = traceAreasCollection.CreateNewElement("add");
addElement1.Properties.Item("provider").Value = "ASPNET";
addElement1.Properties.Item("areas").Value = "Infrastructure,Module,Page,AppServices";
addElement1.Properties.Item("verbosity").Value = "Verbose";
traceAreasCollection.AddElement(addElement1);

var failureDefinitionsElement = addElement.ChildElements.Item("failureDefinitions");
failureDefinitionsElement.Properties.Item("statusCodes").Value = "500";
traceFailedRequestsCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST/Contoso"
Set traceFailedRequestsSection = adminManager.GetAdminSection("system.webServer/tracing/traceFailedRequests", "MACHINE/WEBROOT/APPHOST/Contoso")
Set traceFailedRequestsCollection = traceFailedRequestsSection.Collection

Set addElement = traceFailedRequestsCollection.CreateNewElement("add")
addElement.Properties.Item("path").Value = "*.aspx"
Set traceAreasCollection = addElement.ChildElements.Item("traceAreas").Collection

Set addElement1 = traceAreasCollection.CreateNewElement("add")
addElement1.Properties.Item("provider").Value = "ASPNET"
addElement1.Properties.Item("areas").Value = "Infrastructure,Module,Page,AppServices"
addElement1.Properties.Item("verbosity").Value = "Verbose"
traceAreasCollection.AddElement addElement1

Set failureDefinitionsElement = addElement.ChildElements.Item("failureDefinitions")
failureDefinitionsElement.Properties.Item("statusCodes").Value = "500"
traceFailedRequestsCollection.AddElement addElement

adminManager.CommitChanges()