FTP 日志文件 <logFile>
概述
<logFile>
元素用于配置 FTP 站点的活动日志记录选项。
例如,你可以启用或禁用日志记录、指定要存储在日志文件中的信息类别,以及更改日志文件的存储目录。 还可以使用 <logFile>
元素来控制 IIS 根据日志文件大小或时间间隔创建新日志文件的频率,以及日志文件可以达到的最大大小(以字节为单位)。
注意
与可以使用 IIS、NCSA 或 W3C 格式的日志文件的网站不同,FTP 7 服务仅存储 W3C 格式的日志文件。
可以通过编辑 logExtFileFlags 属性来定义 IIS 记录的信息类别。 默认值为 Date
、Time
、ClientIP
、UserName
、ServerIP
、Method
、UriStem
、FtpStatus
、Win32Status
、FtpSubStatus
、ServerPort
、Session
和 FullPath
。
兼容性
版本 | 说明 |
---|---|
IIS 10.0 | <logFile> 元素在 IIS 10.0 中未进行修改。 |
IIS 8.5 | <logFile> 元素在 IIS 8.5 中未进行修改。 |
IIS 8.0 | <logFile> 元素在 IIS 8.0 中未进行修改。 |
IIS 7.5 | <ftpServer> 元素的 <logFile> 元素作为 IIS 7.5 的功能提供。 |
IIS 7.0 | <ftpServer> 元素的 <logFile> 元素是在 IIS 7.0 的独立下载项 FTP 7.0 中引入的。 |
IIS 6.0 | 空值 |
在 Windows 7 和 Windows Server 2008 R2 中,FTP 7.5 服务作为 IIS 7.5 的功能提供,因此无需再下载 FTP 服务。
安装
若要支持 Web 服务器的 FTP 发布,必须安装 FTP 服务。 为此,请按照以下步骤操作。
Windows Server 2012 或 Windows Server 2012 R2
在任务栏上,单击 “服务器管理器”。
在“服务器管理器”中,单击“管理”菜单,然后单击“添加角色和功能”。
在“添加角色和功能”向导中,单击“下一步”。 选择安装类型,然后单击“下一步”。 选择目标服务器,然后单击“下一步”。
在“服务器角色”页上,展开“Web 服务器(IIS)”,然后选择“FTP 服务器”。
单击“下一步”,然后在“选择功能”页上,再次单击“下一步”。
在“确认安装选择”页上,单击“安装”。
在“结果” 页面中单击“关闭” 。
Windows 8 或 Windows 8.1
在“开始”屏幕上,将指针一直移动到左下角,右键单击“开始”按钮,然后单击“控制面板”。
在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
展开“Internet Information Services”,然后选择“FTP 服务器”。
单击“确定”。
单击“关闭” 。
Windows Server 2008 R2
在任务栏上,单击“开始”,指向“管理工具”,然后单击“服务器管理器”。
在“服务器管理器”层次结构窗格中,展开“角色”,然后单击“Web 服务器(IIS)”。
在“Web 服务器 (IIS)”窗格中,滚动到“角色服务”部分,然后单击“添加角色服务”。
在“添加角色服务向导”的“选择角色服务”页,展开“FTP 服务器”。
选择“FTP 服务”。
单击 “下一步” 。
在“确认安装选择”页中,单击“安装”。
在“结果” 页面中单击“关闭” 。
Windows 7
在任务栏上,单击“开始”,然后单击“控制面板”。
在“控制面板”中,单击“程序与功能”,然后单击“打开或关闭 Windows 功能”。
展开“Internet Information Services”,然后展开“FTP 服务器”。
选择“FTP 服务”。
单击“确定”。
Windows Server 2008 或 Windows Vista
从以下 URL 下载安装包:
按照以下演练中的说明安装 FTP 服务:
操作方式
如何为 FTP 站点配置登录选项
打开 Internet Information Services (IIS) Manager:
如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:
- 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS) Manager”。
如果使用的是 Windows 8 或 Windows 8.1:
- 按住 Windows 键,按字母 X,然后单击“控制面板”。
- 单击“管理工具”,然后双击“Internet Information Services (IIS) Manager”。
如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:
- 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS) Manager”。
如果使用的是 Windows Vista 或 Windows 7:
- 在任务栏上,单击“开始”,然后单击“控制面板”。
- 双击“管理工具”,然后双击“Internet Information Services (IIS) Manager”。
在“连接”窗格中,展开服务器名称,然后转到要在其中配置日志记录的站点。
在“目录”下,指定应存储日志文件的路径。 默认值为 %SystemDrive%\inetpub\logs\LogFiles。
在“日志文件滚动更新”部分,指定以下选项:
计划:根据以下值之一来创建新日志文件:
- 每小时:每小时创建一个新日志文件。
- 每日:每日创建一个新日志文件。
- 每周:每周创建一个新日志文件。
- 每月:每月创建一个新日志文件。
最大文件大小(以字节为单位):在文件达到特定大小(以字节为单位)时创建新日志文件。 最小文件大小是 1048576 字节。 如果将此特性设置为小于 1048576 个字节的值,则会隐式将默认值假定为 1048576 个字节。
不创建新的日志文件:只有一个日志文件,在记录信息的过程中,此文件将不断变大。
选中“使用本地时间进行文件命名和滚动更新”以指定日志文件命名和滚动更新的时间都使用本地服务器时间。 如果未选择此项,则使用协调世界时 (UTC)。
在“操作”窗格中,单击“应用”。
配置
特性
属性 | 说明 | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
directory |
可选的字符串属性。 指定日志记录目录,其中存储日志文件和与日志记录相关的支持文件。 默认值为 %SystemDrive%\inetpub\logs\LogFiles 。 |
||||||||||||||||||||||||||||||||||||||||||||
enabled |
可选布尔属性。 如果启用了日志记录,则为 true;否则为 false。 默认值为 true 。 |
||||||||||||||||||||||||||||||||||||||||||||
localTimeRollover |
可选布尔属性。 如果基于本地时间创建新日志文件,则为 true;否则,基于协调世界时 (UTC)(以前称为格林威治标准时间 (GMT))创建则为 false。 注意:无论设置如何,每个 W3C 扩展日志记录的时间戳都是基于 UTC 的。 默认值为 false 。 |
||||||||||||||||||||||||||||||||||||||||||||
logExtFileFlags |
可选 flags 属性。 指定在记录站点事件期间写入日志文件(使用 W3C 扩展日志文件格式时)或 ODBC 数据源的信息类别。 logExtFileFlags 属性可为以下一个或多个值。 如果指定多个值,请用逗号 (,) 分隔。 默认值为 Date 、Time 、ClientIP 、UserName 、ServerIP 、Method 、UriStem 、FtpStatus 、Win32Status 、FtpSubStatus 、ServerPort 、Session 和 FullPath 。
|
||||||||||||||||||||||||||||||||||||||||||||
period |
可选的枚举属性。 指定 FTP 服务创建新日志文件的频率。 period 属性可为以下值之一。默认值为 Daily 。
|
||||||||||||||||||||||||||||||||||||||||||||
selectiveLogging |
可选 flags 属性。 指定 FTP 日志记录的详细程度。 默认值为 LogSuccessful,LogError,LogInfrastructure 。
|
||||||||||||||||||||||||||||||||||||||||||||
truncateSize |
可选 int64 属性。 指定日志文件的最大大小(以字节为单位),超过该大小后将创建新日志文件。 仅当为 period 属性选择了 MaxSize 时,此值才适用。 最小文件大小是 1,048,576 字节。 如果将此属性设置为小于 1,048,576 字节的值,则会隐式将默认值假定为 1,048,576 字节。默认值为 20971520 。 |
子元素
无。
配置示例
以下配置示例显示了一个 FTP,其中包含自定义日志记录选项,这些选项指定不会记录基础结构命令、日志文件将每天轮换,以及将其他调试选项添加到默认字段。
<site name="ftp.example.com" id="5">
<application path="/">
<virtualDirectory path="/" physicalPath="c:\inetpub\www.example.com" />
</application>
<bindings>
<binding protocol="ftp" bindingInformation="*:21:" />
</bindings>
<ftpServer serverAutoStart="true">
<security>
<authentication>
<basicAuthentication enabled="true" />
<anonymousAuthentication enabled="false" />
</authentication>
</security>
<logFile period="Daily"
logExtFileFlags="Date, Time, ClientIP, UserName, ServerIP, Method, UriStem, FtpStatus, Win32Status, ServerPort, FtpSubStatus, Session, FullPath, Info"
selectiveLogging="LogSuccessful, LogError" />
</ftpServer>
</site>
代码示例
以下示例演示如何为 FTP 站点启用日志记录、为特定路径配置 FTP 站点的日志文件目录,以及指定日志文件的日常回收。
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.logFile.directory:"C:\logs\LogFiles" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.logFile.period:"Daily" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites /[name='ftp.example.com'].ftpServer.logFile.enabled:"True" /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", @"ftp.example.com");
if (siteElement == null) throw new InvalidOperationException("Element not found!");
ConfigurationElement ftpServerElement = siteElement.GetChildElement("ftpServer");
ConfigurationElement logFileElement = ftpServerElement.GetChildElement("logFile");
logFileElement["directory"] = @"C:\logs\LogFiles";
logFileElement["period"] = @"Daily";
logFileElement["enabled"] = true;
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", "ftp.example.com")
If (siteElement Is Nothing) Then
Throw New InvalidOperationException("Element not found!")
End If
Dim ftpServerElement As ConfigurationElement = siteElement.GetChildElement("ftpServer")
Dim logFileElement As ConfigurationElement = ftpServerElement.GetChildElement("logFile")
logFileElement("directory") = "C:\logs\LogFiles"
logFileElement("period") = "Daily"
logFileElement("enabled") = True
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", "ftp.example.com"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);
var ftpServerElement = siteElement.ChildElements.Item("ftpServer");
var logFileElement = ftpServerElement.ChildElements.Item("logFile");
logFileElement.Properties.Item("directory").Value = "C:\\logs\\LogFiles";
logFileElement.Properties.Item("period").Value = "Daily";
logFileElement.Properties.Item("enabled").Value = true;
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 = 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", "ftp.example.com"))
If siteElementPos = -1 Then
Wscript.Echo "Element not found!"
WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)
Set ftpServerElement = siteElement.ChildElements.Item("ftpServer")
Set logFileElement = ftpServerElement.ChildElements.Item("logFile")
logFileElement.Properties.Item("directory").Value = "C:\logs\LogFiles"
logFileElement.Properties.Item("period").Value = "Daily"
logFileElement.Properties.Item("enabled").Value = true
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