0 votes
by (600 points)
edited by

I want to track communication while connecting to SFTP,durign upload/download etc.
where should i et the code to log the communication from client to server via Rebex in the excusing code to get the communication happening.

// start logging to a file
sftp.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt");

Existing cede
Connectivity Code

// create an SFTP object
var sftp = new Rebex.Net.Sftp();
Download code
#region SFTPDownload
private bool SFTP
Download(string FileType, string FileName)
{
string LocalPath = GetFilePath(FileType);
try
{
ExtendedLogger.Log("Downloading SFTP file: " + FileName + " Started!", 4, "SFTP_Download");

           // Common.WriteLogService(" Downloading  SFTP file: " + FileName + " Started!","",false);
            ConnectSftp(ref dSftp);

            if (!(File.Exists(LocalPath + FileName) == true && File.ReadAllBytes(LocalPath + FileName).Length > 0))
            {
                try
                {
                    dSftp.GetFile(GetServerFilePath(FileType).ToString() + "//" + FileName, LocalPath + FileName);
                    ExtendedLogger.Log("Downloading  SFTP file: " + FileName + " Finished!", 4, "SFTP_Download");

                    //Common.WriteLogService(" Downloading  SFTP file: " + FileName + " Finished!", "", false);
                }
                catch (Exception ex)
                {
                    Common.WriteLogService(" Downloading  SFTP file: " + FileName + " Failed!", "", false);
                    Common.WriteLogService(ex.Message,ex.StackTrace,true);
                }
            }
            else
            {
                Common.WriteLogService(" SFTP file: " + FileName + " downloaded already !", "", false);
            }

            return true;
        }
        catch (Exception ex)
        {
            Common.WriteLogService(" Downloading  SFTP file: " + FileName + " Error:" + ex.Message,ex.StackTrace,true);

            if (File.ReadAllBytes(LocalPath + FileName).Length <= 0)
            {
                try
                {
                    dSftp.GetFile(GetServerFilePath(FileType).ToString() + "//OLD//" +FileName, LocalPath + FileName);

                    return true;
                }
                catch 
                {
                    //dSftp.Disconnect();commented by aswathy vm for SFTP Download issue after Server Upgrade - 17112020 
                    return false;
                }
            }
            else
            {
                //dSftp.Disconnect();commented by aswathy vm for SFTP Download issue after Server Upgrade - 17112020 
                return false;
            }                
        }
    }
    #endregion
Applies to: Rebex SFTP

1 Answer

0 votes
by (148k points)

It doesn't really matter where you asing the LogWriter property. Just make sure to assign it before calling the Connect method.
It can be assigned right after calling the constructor:

var sftp = new Rebex.Net.Sftp();
sftp.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt");
by (600 points)
when i tried to debug this code in my file nothing was written.  also can i figure out the network connection issue if any by enabling rebex log.
by (148k points)
Try this code:

    var sftp = new Rebex.Net.Sftp();
    sftp.LogWriter = new Rebex.FileLogWriter(@"C:\MyData\log.txt", LogWriter.Debug);
    sftp.Connect("test.rebex.net");

Does this write anything to the log file?
by (600 points)
now it is logging when i set the sftp object correctly.
can i track the network issue if any by enabling rebex communication logging without using any network analyzer tool
i mean the packets of data we send to server and whether we got response from server etc. I am connecting and uploading etc. using the windows service and issue happening intermittently
by (600 points)
Please respond to the below
can i track the network issue if any by enabling rebex communication log without using any network analyzer tool
by (148k points)
I think I already mostly addressed this question here: https://forum.rebex.net/22824/how-resolve-sftp-connectivity-issue-happening-using-rebex?show=22846#c22846

You can get a communication log at the SSH protocol level from Rebex SFTP if you enable logging at LogLevel.Verbose. But if the issue you are dealing with is still the same ("No connection could be made because the target machine actively refused it" error), you won't see any data packets in the log because no TCP connection has been established, and because raw network packets below TCP level are not accessible to user space applications (Wireshark installs and uses a driver to gain access to raw network traffic).
...