2012-03-12 100 views
4

我想知道是否可以使用FtpWebRequest爲我的FTP客戶端輸出日誌。使用FtpWebRequest輸出日誌

事情是這樣的:

[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()); 
} 

回答

0

檢查這些鏈接如下。如果你有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#)

10

可以使用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

+0

tracemode和maxdatasize屬性是不允許的...... – kaycee 2012-03-12 14:20:13

+0

它說,但沒關係,它應該正常工作。 – svick 2012-03-12 15:39:02

+0

嗨,解決方案看起來不錯,並回答我的期望,但它有可能得到這個信息,而不是寫入文件? – kaycee 2012-03-13 14:48:31