跟踪站点的失败请求日志记录 <traceFailedRequestsLogging>
概述
<site>
元素的 <traceFailedRequestsLogging>
元素设置站点的失败请求跟踪选项,例如失败请求跟踪日志文件的目录、失败请求跟踪日志文件的最大数量,以及是否启用失败请求跟踪。
注意
如果在特定站点的 <siteDefaults>
部分和 <site>
部分中都配置了 <traceFailedRequestsLogging>
元素,则会将 <site>
部分中的配置用于该站点。
注意
<traceFailedRequestsLogging>
元素指定失败请求跟踪的站点级选项,而 <system.webServer/tracing/traceFailedRequests>
元素指定失败请求跟踪规则。
兼容性
版本 | 说明 |
---|---|
IIS 10.0 | <traceFailedRequestsLogging> 元素在 IIS 10.0 中未进行修改。 |
IIS 8.5 | <traceFailedRequestsLogging> 元素在 IIS 8.5 中未进行修改。 |
IIS 8.0 | maxLogFileSizeKB 属性的默认值已增加。 |
IIS 7.5 | <traceFailedRequestsLogging> 元素在 IIS 7.5 中未进行修改。 |
IIS 7.0 | IIS 7.0 中引入了 <site> 元素的 <traceFailedRequestsLogging> 元素。 |
IIS 6.0 | 空值 |
安装
完成 IIS 7 及更高版本的默认安装后,必须安装跟踪角色服务才能使用失败请求跟踪。 安装角色服务后,仍必须在站点级别、应用程序级别或目录级别启用失败请求跟踪。
Windows Server 2012 或 Windows Server 2012 R2
- 在任务栏上,单击 “服务器管理器”。
- 在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
- 在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
- 在“服务器角色”页上,依次展开“Web 服务器(IIS)”、“Web 服务器”和“运行状况和诊断”,然后选择“跟踪”。 单击 “下一步” 。
. - 在“选择功能”页上,单击“下一步”。
- 在“确认安装选择”页上,单击“安装”。
- 在“结果” 页面中单击“关闭” 。
Windows 8 或 Windows 8.1
- 在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”。
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
- 依次展开“Internet Information Services”、“万维网服务”、“运行状况和诊断”,然后选择“跟踪”。
- 单击“确定”。 - 单击“关闭” 。
Windows Server 2008 或 Windows Server 2008 R2
- 在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
- 在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
- 在“Web 服务器(IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
- 在“添加角色服务向导”的“选择角色服务”页上,选择“跟踪”,然后单击“下一步”。
- 在“确认安装选择”页中,单击“安装”。
- 在“结果” 页面中单击“关闭” 。
Windows Vista 或 Windows 7
- 在任务栏上,单击“开始”,然后单击“控制面板”。
- 在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
- 依次展开“Internet Information Services”、“万维网服务”、“运行状况和诊断”。
- 选择“跟踪”,然后单击“确定”。
操作方式
如何配置站点的失败请求跟踪设置
打开 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)管理器”。
在“连接”窗格中,展开服务器名称,展开“站点”节点,然后单击站点名称。
在站点的“首页”窗格中,单击“操作”窗格中的“失败请求跟踪…”。
在“编辑网站失败请求跟踪设置”对话框中,指定跟踪选项,然后单击“确定”。
配置
特性
属性 | 说明 |
---|---|
customActionsEnabled |
可选布尔属性。 指定是否为失败请求跟踪启用自定义操作。 默认值为 false 。 |
directory |
可选的字符串属性。 指定站点的失败请求跟踪日志记录目录。 默认值为 %SystemDrive%\inetpub\logs\FailedReqLogFiles 。 |
enabled |
可选布尔属性。 指定是为网站启用 (true) 还是禁用 (false) 失败请求跟踪日志记录。 默认值为 false 。 |
maxLogFiles |
可选 uint 属性。 指定要为站点保留的失败请求跟踪日志文件的最大数量。 默认值为 50 。 |
maxLogFileSizeKB |
可选 uint 属性。 指定失败请求跟踪日志的文件大小上限(以 KB 为单位)。 注意:如果失败的请求跟踪日志超过此值,IIS 将按最大文件大小截断日志,并为跟踪事件指定 LOG_FILE_MAX_SIZE_TRUNCATE。。 默认值为 1024 。 |
子元素
无。
配置示例
以下配置示例显示了一个网站,该网站启用了失败请求跟踪的日志记录,以及将保留为历史记录的 10 份日志文件。
<site name="Default Web Site" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/" physicalPath="%SystemDrive%\inetpub\wwwroot" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:80:" />
</bindings>
<traceFailedRequestsLogging enabled="true"
directory="%SystemDrive%\inetpub\logs\FailedReqLogFiles"
maxLogFiles="10" />
</site>
代码示例
以下代码示例演示如何为默认网站启用失败请求日志记录,以及如何设置日志记录目录和最大日志文件数。
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].traceFailedRequestsLogging.enabled:True" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].traceFailedRequestsLogging.maxLogFiles:10" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].traceFailedRequestsLogging.directory:%SystemDrive%\inetpub\logs\FailedReqLogFiles" /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 sitesSection = config.GetSection("system.applicationHost/sites");
ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Default Web Site");
if (siteElement == null) throw new InvalidOperationException("Element not found!");
ConfigurationElement traceFailedRequestsLoggingElement = siteElement.GetChildElement("traceFailedRequestsLogging");
traceFailedRequestsLoggingElement["enabled"] = true;
traceFailedRequestsLoggingElement["directory"] = @"%SystemDrive%\inetpub\logs\FailedReqLogFiles";
traceFailedRequestsLoggingElement["maxLogFiles"] = 10;
serverManager.CommitChanges();
}
}
private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
{
foreach (ConfigurationElement element in collection)
{
if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
{
bool matches = true;
for (int i = 0; i < keyValues.Length; i += 2)
{
object o = element.GetAttributeValue(keyValues[i]);
string value = null;
if (o != null)
{
value = o.ToString();
}
if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
{
matches = false;
break;
}
}
if (matches)
{
return element;
}
}
}
return null;
}
}
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 sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Default Web Site")
If (siteElement Is Nothing) Then
Throw New InvalidOperationException("Element not found!")
End If
Dim traceFailedRequestsLoggingElement As ConfigurationElement = siteElement.GetChildElement("traceFailedRequestsLogging")
traceFailedRequestsLoggingElement("enabled") = True
traceFailedRequestsLoggingElement("directory") = "%SystemDrive%\inetpub\logs\FailedReqLogFiles"
traceFailedRequestsLoggingElement("maxLogFiles") = 10
serverManager.CommitChanges()
End Sub
Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
For Each element As ConfigurationElement In collection
If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
Dim matches As Boolean = True
Dim i As Integer
For i = 0 To keyValues.Length - 1 Step 2
Dim o As Object = element.GetAttributeValue(keyValues(i))
Dim value As String = Nothing
If (Not (o) Is Nothing) Then
value = o.ToString
End If
If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
matches = False
Exit For
End If
Next
If matches Then
Return element
End If
End If
Next
Return Nothing
End Function
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 sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "Default Web Site"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);
var traceFailedRequestsLoggingElement = siteElement.ChildElements.Item("traceFailedRequestsLogging");
traceFailedRequestsLoggingElement.Properties.Item("enabled").Value = true;
traceFailedRequestsLoggingElement.Properties.Item("directory").Value = "%SystemDrive%\\inetpub\\logs\\FailedReqLogFiles";
traceFailedRequestsLoggingElement.Properties.Item("maxLogFiles").Value = 10;
adminManager.CommitChanges();
function FindElement(collection, elementTagName, valuesToMatch) {
for (var i = 0; i < collection.Count; i++) {
var element = collection.Item(i);
if (element.Name == elementTagName) {
var matches = true;
for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
var property = element.GetPropertyByName(valuesToMatch[iVal]);
var value = property.Value;
if (value != null) {
value = value.toString();
}
if (value != valuesToMatch[iVal + 1]) {
matches = false;
break;
}
}
if (matches) {
return i;
}
}
}
return -1;
}
VBScript
Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Default Web Site"))
If (siteElementPos = -1) Then
WScript.Echo "Element not found!"
WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)
Set traceFailedRequestsLoggingElement = siteElement.ChildElements.Item("traceFailedRequestsLogging")
traceFailedRequestsLoggingElement.Properties.Item("enabled").Value = True
traceFailedRequestsLoggingElement.Properties.Item("directory").Value = "%SystemDrive%\inetpub\logs\FailedReqLogFiles"
traceFailedRequestsLoggingElement.Properties.Item("maxLogFiles").Value = 10
adminManager.CommitChanges()
Function FindElement(collection, elementTagName, valuesToMatch)
For i = 0 To CInt(collection.Count) - 1
Set element = collection.Item(i)
If element.Name = elementTagName Then
matches = True
For iVal = 0 To UBound(valuesToMatch) Step 2
Set property = element.GetPropertyByName(valuesToMatch(iVal))
value = property.Value
If Not IsNull(value) Then
value = CStr(value)
End If
If Not value = CStr(valuesToMatch(iVal + 1)) Then
matches = False
Exit For
End If
Next
If matches Then
Exit For
End If
End If
Next
If matches Then
FindElement = i
Else
FindElement = -1
End If
End Function