Skip to content

Commit

Permalink
Added logging and directory cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
AndMu committed Feb 23, 2018
1 parent 94e074c commit 0e3cb78
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/YiScanner/Downloader/FileDownloader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using NLog;
Expand Down Expand Up @@ -61,6 +62,23 @@ public async Task<DateTime> Download()
}
}

try
{
var directories = Directory.EnumerateDirectories(path, "*", SearchOption.AllDirectories);
foreach (var directory in directories)
{
if (!Directory.EnumerateFileSystemEntries(directory).Any())
{
log.Info("Removing empty: {0}", directory);
Directory.Delete(directory);
}
}
}
catch (Exception ex)
{
log.Error(ex);
}

log.Info("Completed: {0}", path);
var now = DateTime.Now;
lastScanned = now;
Expand Down
2 changes: 1 addition & 1 deletion src/YiScanner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void Main(string[] args)
log.Info("Starting {0} version utility...", Assembly.GetExecutingAssembly().GetName().Version);
foreach (var address in scanner.GetLocalIPAddress())
{
log.Info("Starting on [{0}]", address);
log.Info("Starting on local IP: [{0}]", address);
}

List<Command> commandsList = new List<Command>();
Expand Down
110 changes: 110 additions & 0 deletions src/YiScanner/Server/FtpLogForNLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using FubarDev.FtpServer;
using NLog;

namespace Wikiled.YiScanner.Server
{
public class FtpLogForNLog : IFtpLog
{
private readonly ILogger logger;

private readonly string remoteAddress;

private readonly string remoteIp;

private readonly int? remotePort;

public FtpLogForNLog(FtpConnection connection)
{
logger = LogManager.GetLogger("FubarDev.FtpServer.FtpConnection");
remoteAddress = connection.RemoteAddress.ToString(true);
remoteIp = connection.RemoteAddress.IpAddress;
remotePort = connection.RemoteAddress.IpPort;
}

public FtpLogForNLog(Type type)
{
logger = LogManager.GetLogger(type.FullName);
}

public FtpLogForNLog(string name)
{
logger = LogManager.GetLogger(name);
}

public void Trace(string format, params object[] args)
{
Log(LogLevel.Trace, null, format, args);
}

public void Trace(Exception ex, string format, params object[] args)
{
Log(LogLevel.Trace, ex, format, args);
}

public void Debug(string format, params object[] args)
{
Log(LogLevel.Debug, null, format, args);
}

public void Debug(Exception ex, string format, params object[] args)
{
Log(LogLevel.Debug, ex, format, args);
}

public void Info(string format, params object[] args)
{
Log(LogLevel.Info, null, format, args);
}

public void Info(Exception ex, string format, params object[] args)
{
Log(LogLevel.Info, ex, format, args);
}

public void Warn(string format, params object[] args)
{
Log(LogLevel.Warn, null, format, args);
}

public void Warn(Exception ex, string format, params object[] args)
{
Log(LogLevel.Warn, ex, format, args);
}

public void Error(string format, params object[] args)
{
Log(LogLevel.Error, null, format, args);
}

public void Error(Exception ex, string format, params object[] args)
{
Log(LogLevel.Error, ex, format, args);
}

public void Fatal(string format, params object[] args)
{
Log(LogLevel.Fatal, null, format, args);
}

public void Fatal(Exception ex, string format, params object[] args)
{
Log(LogLevel.Fatal, ex, format, args);
}

private void Log(LogLevel logLevel, Exception ex, string format, params object[] args)
{
var message = args.Length == 0 ? format : string.Format(format, args);
logger.Log(new LogEventInfo(logLevel, logger.Name, message)
{
Properties =
{
["RemoteAddress"] = remoteAddress,
["RemoteIp"] = remoteIp,
["RemotePort"] = remotePort,
},
Exception = ex,
});
}
}
}
23 changes: 23 additions & 0 deletions src/YiScanner/Server/FtpLogManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using FubarDev.FtpServer;

namespace Wikiled.YiScanner.Server
{
public class FtpLogManager : IFtpLogManager
{
public IFtpLog CreateLog(FtpConnection connection)
{
return new FtpLogForNLog(connection);
}

public IFtpLog CreateLog(string name)
{
return new FtpLogForNLog(name);
}

public IFtpLog CreateLog(Type type)
{
return new FtpLogForNLog(type);
}
}
}
3 changes: 2 additions & 1 deletion src/YiScanner/Server/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ public void Dispose()

public void Start()
{
log.Debug("Start");
var outPath = Path.Combine(Environment.CurrentDirectory, config.Path);
outPath.EnsureDirectoryExistence();
log.Debug("Start FTP server: [{0}]", outPath);
var membershipProvider = new AnonymousMembershipProvider(new NoValidation());
var provider = new DotNetFileSystemProvider(outPath, false);

// Initialize the FTP server
ftpServer = new FtpServer(provider, membershipProvider, "127.0.0.1", config.Port, new AssemblyFtpCommandHandlerFactory(typeof(FtpServer).GetTypeInfo().Assembly));
ftpServer.LogManager = new FtpLogManager();

// Start the FTP server
ftpServer.Start();
Expand Down

0 comments on commit 0e3cb78

Please sign in to comment.