Skip to content

Commit 0e3cb78

Browse files
committed
Added logging and directory cleanup
1 parent 94e074c commit 0e3cb78

5 files changed

Lines changed: 154 additions & 2 deletions

File tree

src/YiScanner/Downloader/FileDownloader.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Net;
45
using System.Threading.Tasks;
56
using NLog;
@@ -61,6 +62,23 @@ public async Task<DateTime> Download()
6162
}
6263
}
6364

65+
try
66+
{
67+
var directories = Directory.EnumerateDirectories(path, "*", SearchOption.AllDirectories);
68+
foreach (var directory in directories)
69+
{
70+
if (!Directory.EnumerateFileSystemEntries(directory).Any())
71+
{
72+
log.Info("Removing empty: {0}", directory);
73+
Directory.Delete(directory);
74+
}
75+
}
76+
}
77+
catch (Exception ex)
78+
{
79+
log.Error(ex);
80+
}
81+
6482
log.Info("Completed: {0}", path);
6583
var now = DateTime.Now;
6684
lastScanned = now;

src/YiScanner/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void Main(string[] args)
4141
log.Info("Starting {0} version utility...", Assembly.GetExecutingAssembly().GetName().Version);
4242
foreach (var address in scanner.GetLocalIPAddress())
4343
{
44-
log.Info("Starting on [{0}]", address);
44+
log.Info("Starting on local IP: [{0}]", address);
4545
}
4646

4747
List<Command> commandsList = new List<Command>();
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using FubarDev.FtpServer;
3+
using NLog;
4+
5+
namespace Wikiled.YiScanner.Server
6+
{
7+
public class FtpLogForNLog : IFtpLog
8+
{
9+
private readonly ILogger logger;
10+
11+
private readonly string remoteAddress;
12+
13+
private readonly string remoteIp;
14+
15+
private readonly int? remotePort;
16+
17+
public FtpLogForNLog(FtpConnection connection)
18+
{
19+
logger = LogManager.GetLogger("FubarDev.FtpServer.FtpConnection");
20+
remoteAddress = connection.RemoteAddress.ToString(true);
21+
remoteIp = connection.RemoteAddress.IpAddress;
22+
remotePort = connection.RemoteAddress.IpPort;
23+
}
24+
25+
public FtpLogForNLog(Type type)
26+
{
27+
logger = LogManager.GetLogger(type.FullName);
28+
}
29+
30+
public FtpLogForNLog(string name)
31+
{
32+
logger = LogManager.GetLogger(name);
33+
}
34+
35+
public void Trace(string format, params object[] args)
36+
{
37+
Log(LogLevel.Trace, null, format, args);
38+
}
39+
40+
public void Trace(Exception ex, string format, params object[] args)
41+
{
42+
Log(LogLevel.Trace, ex, format, args);
43+
}
44+
45+
public void Debug(string format, params object[] args)
46+
{
47+
Log(LogLevel.Debug, null, format, args);
48+
}
49+
50+
public void Debug(Exception ex, string format, params object[] args)
51+
{
52+
Log(LogLevel.Debug, ex, format, args);
53+
}
54+
55+
public void Info(string format, params object[] args)
56+
{
57+
Log(LogLevel.Info, null, format, args);
58+
}
59+
60+
public void Info(Exception ex, string format, params object[] args)
61+
{
62+
Log(LogLevel.Info, ex, format, args);
63+
}
64+
65+
public void Warn(string format, params object[] args)
66+
{
67+
Log(LogLevel.Warn, null, format, args);
68+
}
69+
70+
public void Warn(Exception ex, string format, params object[] args)
71+
{
72+
Log(LogLevel.Warn, ex, format, args);
73+
}
74+
75+
public void Error(string format, params object[] args)
76+
{
77+
Log(LogLevel.Error, null, format, args);
78+
}
79+
80+
public void Error(Exception ex, string format, params object[] args)
81+
{
82+
Log(LogLevel.Error, ex, format, args);
83+
}
84+
85+
public void Fatal(string format, params object[] args)
86+
{
87+
Log(LogLevel.Fatal, null, format, args);
88+
}
89+
90+
public void Fatal(Exception ex, string format, params object[] args)
91+
{
92+
Log(LogLevel.Fatal, ex, format, args);
93+
}
94+
95+
private void Log(LogLevel logLevel, Exception ex, string format, params object[] args)
96+
{
97+
var message = args.Length == 0 ? format : string.Format(format, args);
98+
logger.Log(new LogEventInfo(logLevel, logger.Name, message)
99+
{
100+
Properties =
101+
{
102+
["RemoteAddress"] = remoteAddress,
103+
["RemoteIp"] = remoteIp,
104+
["RemotePort"] = remotePort,
105+
},
106+
Exception = ex,
107+
});
108+
}
109+
}
110+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using FubarDev.FtpServer;
3+
4+
namespace Wikiled.YiScanner.Server
5+
{
6+
public class FtpLogManager : IFtpLogManager
7+
{
8+
public IFtpLog CreateLog(FtpConnection connection)
9+
{
10+
return new FtpLogForNLog(connection);
11+
}
12+
13+
public IFtpLog CreateLog(string name)
14+
{
15+
return new FtpLogForNLog(name);
16+
}
17+
18+
public IFtpLog CreateLog(Type type)
19+
{
20+
return new FtpLogForNLog(type);
21+
}
22+
}
23+
}

src/YiScanner/Server/ServerManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ public void Dispose()
3333

3434
public void Start()
3535
{
36-
log.Debug("Start");
3736
var outPath = Path.Combine(Environment.CurrentDirectory, config.Path);
3837
outPath.EnsureDirectoryExistence();
38+
log.Debug("Start FTP server: [{0}]", outPath);
3939
var membershipProvider = new AnonymousMembershipProvider(new NoValidation());
4040
var provider = new DotNetFileSystemProvider(outPath, false);
4141

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

4546
// Start the FTP server
4647
ftpServer.Start();

0 commit comments

Comments
 (0)