private void SendAllStoredCrashReports() { if (!HasInternetConnection) { RaygunLogger.Debug("Not sending stored crash reports due to no internet connection"); return; } // Get all stored crash reports. var reports = _fileManager.GetAllStoredCrashReports(); RaygunLogger.Debug(string.Format("Attempting to send {0} stored crash report(s)", reports.Count)); // Quick escape if there's no crash reports. if (reports.Count == 0) { return; } // Run on another thread. Task.Run(async() => { // Use a single HttpClient for all requests. using (var client = new HttpClient()) { foreach (var report in reports) { try { RaygunLogger.Verbose("Sending JSON -------------------------------"); RaygunLogger.Verbose(report.Data); RaygunLogger.Verbose("--------------------------------------------"); // Create the request contnet. HttpContent content = new StringContent(report.Data, System.Text.Encoding.UTF8, "application/json"); // Add API key to headers. content.Headers.Add("X-ApiKey", _apiKey); // Perform the request. var response = await client.PostAsync(RaygunSettings.Settings.ApiEndpoint, content); // Check the response. var statusCode = (int)response.StatusCode; RaygunLogger.LogResponseStatusCode(statusCode); // Remove the stored crash report if it was sent successfully. if (statusCode == (int)RaygunResponseStatusCode.Accepted) { _fileManager.RemoveFile(report.Path); // We can delete the file from disk now. } } catch (Exception e) { RaygunLogger.Error("Failed to send stored crash report due to error: " + e.Message); } } } }); }
private async Task SendStoredReportAsync(HttpClient client, RaygunFile report) { try { RaygunLogger.Verbose("Sending JSON -------------------------------"); RaygunLogger.Verbose(report.Data); RaygunLogger.Verbose("--------------------------------------------"); // Create the request contnet. HttpContent content = new StringContent(report.Data, System.Text.Encoding.UTF8, "application/json"); // Add API key to headers. content.Headers.Add("X-ApiKey", _apiKey); // Perform the request. var response = await client.PostAsync(RaygunSettings.Settings.ApiEndpoint, content); // Check the response. var statusCode = (int)response.StatusCode; RaygunLogger.LogResponseStatusCode(statusCode); // Remove the stored crash report if it was sent successfully. if (statusCode == (int)RaygunResponseStatusCode.Accepted) { _fileManager.RemoveFile(report.Path); // We can delete the file from disk now. } } catch (Exception e) { RaygunLogger.Error("Failed to send stored crash report due to error: " + e.Message); } }
private void Send(RaygunMessage raygunMessage, int timeout) { if (!ValidateApiKey()) { RaygunLogger.Error("Failed to send due to invalid API key"); return; } bool canSend = OnSendingMessage(raygunMessage); if (!canSend) { RaygunLogger.Debug("Sending message cancelled"); return; } // No internet then we store the report. if (!HasInternetConnection) { var path = _fileManager.SaveCrashReport(raygunMessage, MaxReportsStoredOnDevice); if (!string.IsNullOrEmpty(path)) { RaygunLogger.Debug("Saved crash report to: " + path); } return; } try { // Create the json data. var jsonData = SimpleJson.SerializeObject(raygunMessage); var statusCode = SendMessage(jsonData, timeout); RaygunLogger.LogResponseStatusCode(statusCode); // Save the message if the application is currently being rate limited or there was a timeout. if (statusCode == (int)RaygunResponseStatusCode.RateLimited || statusCode == (int)RaygunResponseStatusCode.RequestTimeout || statusCode == (int)RaygunResponseStatusCode.GatewayTimeout) { var path = _fileManager.SaveCrashReport(raygunMessage, MaxReportsStoredOnDevice); if (!string.IsNullOrEmpty(path)) { RaygunLogger.Debug("Saved crash report to: " + path); } } } catch (Exception e) { RaygunLogger.Error(string.Format("Failed to send message due to error {0}: {1}", e.GetType().Name, e.Message)); var path = _fileManager.SaveCrashReport(raygunMessage, MaxReportsStoredOnDevice); if (!string.IsNullOrEmpty(path)) { RaygunLogger.Debug("Saved crash report to: " + path); } } }