4

I couldn't find any information on how to do it. Basically FluentFTP is using System.Diagnostics to log their messages. FluentFtp expose the following static method:

FtpTrace.AddListener(TraceListener listener);

However I don't know if there is any way to implement (or use existing implementation, which?) TraceListener in the way it relays everything to log4net engine.

Any hints or ideas?

Thanks, Radek

2 Answers 2

4

You can attach a listener to the OnLogEvent method that FluentFTP exposes.

private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

public static void UploadFTP(FileInfo localFile, string remoteFileLocation, string remoteServer, NetworkCredential credentials)
{
    FtpClient client = new FtpClient(remoteServer, credentials);
    client.RetryAttempts = 3;
    client.OnLogEvent = OnFTPLogEvent;

    client.Connect();

    if (!client.UploadFile(localFile.FullName, remoteFileLocation, FtpExists.Overwrite, false, FtpVerify.Retry | FtpVerify.Throw))
    {
        throw new Exception($"Could not Upload File {localFile.Name}. See Logs for more information");
    }
}

private static void OnFTPLogEvent(FtpTraceLevel ftpTraceLevel, string logMessage)
{
    switch (ftpTraceLevel)
    {
        case FtpTraceLevel.Error:
            Log.Error(logMessage);
            break;
        case FtpTraceLevel.Verbose:
            Log.Debug(logMessage);
            break;
        case FtpTraceLevel.Warn:
            Log.Warn(logMessage);
            break;
        case FtpTraceLevel.Info:
        default:
            Log.Info(logMessage);
            break;
    }
}

The method OnFTPLogEvent will be called every-time the OnLogEvent action will be called allowing you to extend any logging you have already built into your application.

3

Basically FluentFTP is using System.Diagnostics.TraceListener so in order to make it logging to your log4net log you need to write your own simple class that would redirect logs to log4net logger. Like the following:

using System.Diagnostics; using log4net;

namespace YourApp.Logging
{
    public class Log4NetTraceListener : TraceListener
    {
        private readonly ILog _log;

        public Log4NetTraceListener(string provider)
        {
            _log = LogManager.GetLogger(provider);
        }

        public override void Write(string message)
        {
            if(_log == null)
                return;
            if(!string.IsNullOrWhiteSpace(message))
                _log.Info(message);
        }

        public override void WriteLine(string message)
        {
            if(_log == null)
                return;
            if (!string.IsNullOrWhiteSpace(message))
                _log.Info(message);
        }
    }
}

Then, in your app.config file add the following entry:

  <system.diagnostics>
    <trace autoflush="true"></trace>
    <sources>
      <source name="FluentFTP">
        <listeners>
          <clear />
          <add name="FluentLog" />
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="FluentLog" type="YourApp.Logging.Log4NetTraceListener, YourApp" initializeData="FluentLog" />
    </sharedListeners>
  </system.diagnostics>  

That should enable FluentFtp logs and merge it with your application log4net log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.