public async Task <SyncResult> Sync(HiDriveSyncExecutionInfo syncExecutionInfo, CancellationToken token) { var result = new SyncResult(syncExecutionInfo); Directory.CreateDirectory(syncExecutionInfo.LogPath); using (_logger = new LoggerConfiguration().MinimumLevel.Verbose().WriteTo.File(Path.Combine(syncExecutionInfo.LogPath, $"{syncExecutionInfo.Name}_Log_{DateTime.Now:yyyy_MM_dd_HH_mm_ss}.txt"), syncExecutionInfo.LogEventLevel).WriteTo.Debug().CreateLogger()) { _logger.Information("Starting Sync of '{LocalDirectory}' to '{RemotePath}'", syncExecutionInfo.SourcePath, syncExecutionInfo.DestinationPath); result.Start = DateTime.Now; try { var destination = await CreateDirectoryPath(syncExecutionInfo.DestinationPath, token); var source = new DirectoryInfo(syncExecutionInfo.SourcePath); result.Items = await SyncDirectory(destination, source, token); result.State = SyncState.Successful; } catch (OperationCanceledException operationCanceledException) { _logger.Warning(operationCanceledException, "Sync Process was cancelled!"); result.Items = new List <ItemSyncResult>(); result.State = SyncState.Cancelled; } catch (Exception exception) { _logger.Fatal(exception, ""); result.Items = new List <ItemSyncResult>(); result.State = SyncState.Failed; result.Exception = exception; } finally { result.End = DateTime.Now; } } return(result); }
private async Task SendEmail(SyncResult syncResult) { var smtpAccount = _accountService.Accounts.SmtpAccounts.FirstOrDefault(account => account.Id == _folderConfiguration.NotificationConfiguration.EmailConfigurationId); if (smtpAccount != null) { var body = new StringBuilder(); body.AppendLine(syncResult.TaskName); body.AppendLine($"State: {syncResult.State}"); body.AppendLine(); body.AppendLine($"Started at: {syncResult.Start}"); body.AppendLine($"Finished at: {syncResult.End}"); body.AppendLine(); body.AppendLine($"Source: {_folderConfiguration.SourcePath}"); body.AppendLine($"Destination: {_folderConfiguration.DestinationPath}"); body.AppendLine(); if (syncResult.Exception != null) { body.AppendLine($"Exception: {syncResult.Exception}"); body.AppendLine(); } else { body.AppendLine("Summary:"); body.AppendLine($"Total:\t{syncResult.Items.Count}"); body.AppendLine($"Added:\t{syncResult.Items.Count(result => result.Action == SyncAction.Added && result.State == SyncState.Successful)}"); body.AppendLine($"Updated:\t{syncResult.Items.Count(result => result.Action == SyncAction.Updated && result.State == SyncState.Successful)}"); body.AppendLine($"Unchanged:\t{syncResult.Items.Count(result => result.Action == SyncAction.None && result.State == SyncState.Successful)}"); body.AppendLine($"Failed:\t{syncResult.Items.Count(result => result.State == SyncState.Failed)}"); } body.AppendLine("Items:"); foreach (var resultItem in syncResult.Items) { if (resultItem.Action == SyncAction.None) { continue; } if (resultItem.State == SyncState.Successful) { body.AppendLine( $"\t{resultItem.Action}: {resultItem.Name}"); } else { body.AppendLine( $"\t{resultItem.Action}: {resultItem.Name} {resultItem.State} {resultItem.Exception}"); } } try { var mimeMessage = new MimeMessage(); mimeMessage.From.Add(new MailboxAddress("SyncService", "*****@*****.**")); mimeMessage.To.Add(new MailboxAddress(smtpAccount.EmailTo)); mimeMessage.Subject = $"[Sync] {syncResult.TaskName} - {syncResult.State}"; mimeMessage.Body = new TextPart("plain") { Text = body.ToString() }; using (var client = new SmtpClient()) { await client.ConnectAsync(smtpAccount.Server, smtpAccount.Port); await client.AuthenticateAsync(smtpAccount.Username, smtpAccount.Password); await client.SendAsync(mimeMessage); } } catch (Exception exception) { Log.Error(exception, "Failure while sending email"); } } }