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); } } } }); }
internal int SendMessage(string message) { RaygunLogger.Verbose("Sending JSON -------------------------------"); RaygunLogger.Verbose(message); RaygunLogger.Verbose("--------------------------------------------"); using (var client = new WebClient()) { client.Headers.Add("X-ApiKey", _apiKey); client.Headers.Add("content-type", "application/json; charset=utf-8"); client.Encoding = System.Text.Encoding.UTF8; try { client.UploadString(RaygunSettings.Settings.ApiEndpoint, message); } catch (Exception e) { RaygunLogger.Error(string.Format("Error Logging Exception to Raygun.io {0}", e.Message)); if (e.GetType().Name == "WebException") { WebException we = (WebException)e; HttpWebResponse response = (HttpWebResponse)we.Response; return((int)response.StatusCode); } } } return((int)HttpStatusCode.Accepted); }
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); } }
internal int SendMessage(string message, int timeout) { RaygunLogger.Verbose("Sending JSON -------------------------------"); RaygunLogger.Verbose(message); RaygunLogger.Verbose("--------------------------------------------"); int statusCode = 0; using (var client = new HttpClient()) { try { // Set the timeout if (timeout > 0) { client.Timeout = TimeSpan.FromMilliseconds(timeout); } // Create the request contnet. HttpContent content = new StringContent(message, System.Text.Encoding.UTF8, "application/json"); // Add API key to headers. content.Headers.Add("X-ApiKey", _apiKey); // Perform the request. var task = client.PostAsync(RaygunSettings.Settings.ApiEndpoint, content); task.Wait(); var response = task.Result; // Check the response. statusCode = (int)response.StatusCode; } catch (Exception e) { RaygunLogger.Error(string.Format("Failed to send message due to error {0}: {1}", e.GetType().Name, e.Message)); // Was this due to the request timing out? const string TaskCanceledEx = "TaskCanceledException"; if (e.GetType().Name == TaskCanceledEx || e.InnerException.GetType().Name == TaskCanceledEx) { statusCode = (int)HttpStatusCode.RequestTimeout; } else { statusCode = (int)HttpStatusCode.BadRequest; } } } return(statusCode); }