我找不到任何关于如何做的信息。基本上,FluentFTP使用System.Diagnostics来记录他们的消息。FluentFtp公开以下静态方法:
FtpTrace.AddListener(TraceListener listener);然而,我不知道是否有任何方法来实现(或使用现有的实现,哪种?)它将所有东西都转发给log4net引擎的方式是TraceListener。
有什么提示或想法吗?
谢谢,Radek
发布于 2019-07-04 18:41:40
可以将侦听器附加到FluentFTP公开的OnLogEvent方法。
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;
}
}每次调用OnLogEvent操作时,都会调用OnFTPLogEvent方法,从而允许您扩展已经构建到应用程序中的任何日志记录。
发布于 2018-04-19 01:05:19
基本上,FluentFTP使用的是System.Diagnostics.TraceListener,所以为了将其记录到您的log4net日志中,您需要编写自己的简单类,将日志重定向到log4net记录器。如下所示:
使用System.Diagnostics;使用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);
}
}
}然后,在app.config文件中添加以下条目:
<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> 这将启用FluentFtp日志并将其与您的应用程序log4net日志合并。
https://stackoverflow.com/questions/49794118
复制相似问题