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); } }