Skip to content

Commit b6a91c5

Browse files
committed
Log error
1 parent 4247371 commit b6a91c5

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

AutoSSHApp.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ private static long BackupFile(string root, string remotePath, SftpClient client
321321
return 0;
322322
}
323323

324-
private static long BackupFolder(HostEntry host, string root, string path, SftpClient client)
324+
private static long BackupFolder(HostEntry host, string root, string path, SftpClient client, StreamWriter log)
325325
{
326326
long size = 0;
327327
foreach (string fileOrFolder in path.Split('|').Select(s => s.Trim()).Where(s => s.Length != 0))
@@ -341,15 +341,22 @@ private static long BackupFolder(HostEntry host, string root, string path, SftpC
341341
}
342342
else
343343
{
344-
SftpFile[] files = client.ListDirectory(fileOrFolder).Where(f => f.IsRegularFile || (f.IsDirectory && !f.Name.StartsWith("."))).ToArray();
345-
Parallel.ForEach(files.Where(f => f.IsRegularFile && (host.IgnoreRegex == null || !host.IgnoreRegex.IsMatch(f.FullName))), parallelOptions, (_file) =>
344+
try
346345
{
347-
Interlocked.Add(ref size, BackupFile(root, _file.FullName, client));
348-
});
349-
Parallel.ForEach(files.Where(f => f.IsDirectory), parallelOptions2, (folder) =>
346+
SftpFile[] files = client.ListDirectory(fileOrFolder).Where(f => f.IsRegularFile || (f.IsDirectory && !f.Name.StartsWith("."))).ToArray();
347+
Parallel.ForEach(files.Where(f => f.IsRegularFile && (host.IgnoreRegex == null || !host.IgnoreRegex.IsMatch(f.FullName))), parallelOptions, (_file) =>
348+
{
349+
Interlocked.Add(ref size, BackupFile(root, _file.FullName, client));
350+
});
351+
Parallel.ForEach(files.Where(f => f.IsDirectory), parallelOptions2, (folder) =>
352+
{
353+
Interlocked.Add(ref size, BackupFolder(host, root, folder.FullName, client, log));
354+
});
355+
}
356+
catch (Exception ex)
350357
{
351-
Interlocked.Add(ref size, BackupFolder(host, root, folder.FullName, client));
352-
});
358+
log.WriteLine("Failed to backup file or folder {0}, error: {1}", fileOrFolder, ex.Message);
359+
}
353360
}
354361
}
355362
return size;
@@ -401,7 +408,7 @@ private static void ClientLoop(string root, HostEntry host, List<string> command
401408
{
402409
if (command.StartsWith("$backup ", StringComparison.OrdinalIgnoreCase))
403410
{
404-
backupSize += BackupFolder(host, backupPath, command.Substring(8), sftpClient);
411+
backupSize += BackupFolder(host, backupPath, command.Substring(8), sftpClient, writer);
405412
}
406413
else if (command.StartsWith("$ignore ", StringComparison.OrdinalIgnoreCase))
407414
{

0 commit comments

Comments
 (0)