public static async void SendAllLogs() { UpdateCorrelationId(null); ILoggingManager manager = ServiceLocator.Current.GetInstance <ILoggingManager>(); try { bool allLogsSent = false; while (!allLogsSent) { List <LogSQLiteModel> logs = await manager.GetLogs(_numLogsToSendAtATime); if (logs == null || !logs.Any()) { break; } // Try to post logs to the server List <LogDTO> dto = logs.Select(l => new LogDTO(l)).ToList(); bool success = await(new LoggingService()).PostAllLogs(dto); // If posting succeeded, delete them if (success) { await manager.DeleteLogs(logs); } // Else, we must try to send them another time. // Also make sure we don't store too many logs because sending keeps failing else { DeleteLogsIfTooMany(); break; } allLogsSent = logs.Count < _numLogsToSendAtATime; } } catch (Exception e) { System.Diagnostics.Debug.Print($"{nameof(LogUtils)}.{nameof(SendAllLogs)}: Failed to send logs. Wiping DB to prevent deadlock"); System.Diagnostics.Debug.Print(e.ToString()); await manager.DeleteAll(); } }
private static async void DeleteLogsIfTooMany() { ILoggingManager manager = ServiceLocator.Current.GetInstance <ILoggingManager>(); bool tooManyPersistedLogs = true; while (tooManyPersistedLogs) { // GetLogs gives the earliest logs List <LogSQLiteModel> logs = await manager.GetLogs(_maxNumOfPersistedLogsOnSendError * 2); tooManyPersistedLogs = logs.Count() > _maxNumOfPersistedLogsOnSendError; if (tooManyPersistedLogs) { int atLeastNLogsTooMany = logs.Count() - _maxNumOfPersistedLogsOnSendError; List <LogSQLiteModel> logsToDelete = logs.Take(atLeastNLogsTooMany).ToList(); await manager.DeleteLogs(logsToDelete); } } }