FTP Logging to File AND Console

+1 vote
asked Dec 14, 2015 by thomen (160 points)

Hi I was wondering how I would go about logging to file and console at the same time?

Thanks very much

Applies to: Rebex FTP/SSL

1 Answer

+1 vote
answered Dec 14, 2015 by Lukas Matyska (47,150 points)
selected Dec 14, 2015 by thomen
 
Best answer

The simplest solution is to write new ILogWriter which logs into specified underlying loggers. The source code is here:

public class MultiLogWriter : ILogWriter
{
    ILogWriter[] _loggers;

    public MultiLogWriter(params ILogWriter[] loggers)
    {
        if (loggers == null)
            throw new ArgumentNullException("loggers");
        _loggers = loggers;
    }

    public LogLevel Level
    {
        get
        {
            // return the most informative level
            LogLevel level = LogLevel.Off;
            foreach (var logger in _loggers)
            {
                // smaller level means more informative
                if (logger.Level < level)
                    level = logger.Level;
            }
            return level;
        }
        set
        {
            foreach (var logger in _loggers)
            {
                logger.Level = value;
            }
        }
    }

    public void Write(LogLevel level, Type objectType, int objectId, string area, string message, byte[] buffer, int offset, int length)
    {
        foreach (var logger in _loggers)
        {
            logger.Write(level, objectType, objectId, area, message, buffer, offset, length);
        }
    }

    public void Write(LogLevel level, Type objectType, int objectId, string area, string message)
    {
        foreach (var logger in _loggers)
        {
            logger.Write(level, objectType, objectId, area, message);
        }
    }
}

You can use it like this:

client.LogWriter = new MultiLogWriter(
    new ConsoleLogWriter(LogLevel.Debug), 
    new FileLogWriter("path", LogLevel.Debug));
commented Dec 14, 2015 by thomen (160 points)
Thanks for a super detailed answer I really appreciate it! :) VOTED!
...