使用 FtpWebRequest 输出日志

Posted

技术标签:

【中文标题】使用 FtpWebRequest 输出日志【英文标题】:Output log using FtpWebRequest 【发布时间】:2012-03-28 17:02:03 【问题描述】:

我想知道是否可以使用FtpWebRequest 为我的 FTP 客户端输出日志。

类似这样的:

[R] USER xxx
[R] 331 Please specify the password.
[R] PASS (hidden)
[R] 230 Login successful.
[R] SYST
[R] 215 UNIX Type: L8
[R] FEAT
[R] 211-Features:
[R]  EPRT
[R]  EPSV
[R]  MDTM
[R]  PASV
[R]  REST STREAM
[R]  SIZE
[R]  TVFS
[R] 211 End
[R] PWD
[R] 257 "/"
[R] CWD /
[R] 250 Directory successfully changed.
[R] PWD
[R] 257 "/"
[R] TYPE A
[R] 200 Switching to ASCII mode.
[R] PASV
[R] 227 Entering Passive Mode (10,232,201,81,141,175)
[R] Opening data connection IP: 10.232.201.81 PORT: 36271
[R] LIST -al
[R] 150 Here comes the directory listing.
[R] 226 Directory send OK.

这个输出例如是连接时...

我当前的代码仅执行以下操作:

// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(string.Format("ftp://0", addrEndPoint));
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(_currentConnection.Username, _currentConnection.Password);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

Stream responseStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);

if (readStream != null)

    Console.WriteLine(readStream.ReadToEnd());

【问题讨论】:

【参考方案1】:

您可以使用Network Tracing 执行此操作。到set it up,创建(或修改,如果你已经有)App.config 文件,使它看起来像这样(如果你已经有文件,你需要添加设置):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="System.Net" tracemode="protocolonly" maxdatasize="1024">
        <listeners>
          <add name="System.Net"/>
        </listeners>
      </source>
    </sources>
    <switches>
      <add name="System.Net" value="Information"/>
    </switches>
    <sharedListeners>
      <add name="System.Net"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="network.log"
      />
    </sharedListeners>
    <trace autoflush="true"/>
  </system.diagnostics>
</configuration>

如果您这样做,您的应用程序将创建一个network.log 文件,可能看起来像这样:

System.Net Information: 1 : [8892] FtpWebRequest#2383799::.ctor(ftp://test/)
System.Net Information: 0 : [8892] FtpWebRequest#2383799::GetResponse(Method=LIST.)
System.Net Information: 0 : [8892] Current OS installation type is 'Client'.
System.Net Information: 0 : [8892] RAS supported: True
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Created connection from 192.168.1.1:51833 to 192.168.1.2:21.
System.Net Information: 0 : [8892] Associating FtpWebRequest#2383799 with FtpControlStream#33675143
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [220 This is the test FTP server. Authentication required.]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Sending command [USER svick]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [331 Password required for svick]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Sending command [PASS ********]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [230 Logged on]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [200 UTF8 mode enabled]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Sending command [PWD]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [257 "/" is current directory.]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Sending command [TYPE I]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [200 Type set to I]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Sending command [PASV]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [227 Entering Passive Mode (174,37,88,92,117,98)]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Sending command [LIST]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [150 Connection accepted]
System.Net Information: 0 : [8892] FtpControlStream#33675143 - Received response [226 Transfer OK]
System.Net Information: 0 : [8892] FtpWebRequest#2383799::(Releasing FTP connection#33675143.)

它非常冗长,但确实包含您需要的信息。如果要修改日志文件的写入方式,可以实现自己的TraceListener,并在配置文件中使用它而不是TextWriterTraceListener

【讨论】:

上面说了,不过没关系,应该可以正常工作的。 您好,该解决方案看起来不错并满足了我的期望,但是是否可以获取此信息而不是写入文件? 是的,我想你会创建一个继承自 TraceListener 的类来做到这一点,并在配置文件中使用它。 日志文件在哪里创建?【参考方案2】:

查看以下链接以供参考。如果您有 IIS 7,则可以实现自己的功能。

implementing IFtpLogProvider interface's Log method

using System;
using System.IO;
using Microsoft.Web.FtpServer;
using System.Diagnostics;
using System.Diagnostics.Eventing;

namespace FtpLogging

    public class FtpLogDemo : BaseProvider,
        IFtpLogProvider
    
        void IFtpLogProvider.Log(FtpLogEntry loggingParameters)
        
         .......

参考:IIS FTP 7.5 Extensibility (IFtpLogProvider and logging FTP failures to the event log)Problem writing to a file (C#)

【讨论】:

以上是关于使用 FtpWebRequest 输出日志的主要内容,如果未能解决你的问题,请参考以下文章

FtpWebRequest/FtpWebResponse 上的 OPTS 命令

使用 FtpWebRequest 附加到大型机上的文件时出现问题

FtpWebRequest打开随机端口而不是21

FtpWebRequest 测试环境连接失败

为啥 FtpWebRequest 恰好在长传输结束时抛出 WebException?

FtpWebRequest 下载文件