Output log using FtpWebRequest

后端 未结 2 2181
借酒劲吻你
借酒劲吻你 2020-11-21 23:54

I wonder if it\'s possible to have output log for my FTP client using FtpWebRequest.

Something like this:



        
相关标签:
2条回答
  • 2020-11-22 00:34

    check these following links for reference. if you have IIS 7 then it is possible to implement your own functionality.

    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)
            {
             .......
    

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

    0 讨论(0)
  • 2020-11-22 00:47

    You can do this using Network Tracing. To set it up, create (or modify, if you already have one) App.config file, so that it looks like this (if you already have the file, you will need to add the settings to it):

    <?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>
    

    If you do this, your application will create a network.log file, that might look something like this:

    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.)
    

    It's quite verbose, but it does contain the information you need. If you want to modify how is the log file written, you can implement your own TraceListener and use that in the config file instead of TextWriterTraceListener.

    0 讨论(0)
提交回复
热议问题