コード例 #1
0
ファイル: RaygunClient.cs プロジェクト: huangskar/raygun4net
        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);
                        }
                    }
                }
            });
        }
コード例 #2
0
ファイル: RaygunClient.cs プロジェクト: thoemmi/raygun4net
        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.Info(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;
            }

            try
            {
                Task.Run(async() =>
                {
                    // Use a single HttpClient for all requests.
                    using (var client = new HttpClient())
                    {
                        foreach (var report in reports)
                        {
                            await SendStoredReportAsync(client, report);
                        }
                    }
                }).ContinueWith(t =>
                {
                    if (t != null && t.IsFaulted)
                    {
                        RaygunLogger.Error("Fault occurred when sending stored reports - clearing stored reports");

                        try
                        {
                            // If there was an issue then clear the stored reports.
                            _fileManager.RemoveFiles(reports);
                        }
                        catch (Exception e)
                        {
                            RaygunLogger.Error("Failed to remove stored report due to error: " + e.Message);
                        }
                    }

                    if (t != null && t.Exception != null)
                    {
                        // Consume all errors as we dont want them being sent.
                        t.Exception.Handle((e) =>
                        {
                            RaygunLogger.Error("Error occurred while sending stored reports: " + e.Message);
                            return(true); // Handled
                        });
                    }
                });
            }
            catch (Exception e)
            {
                RaygunLogger.Error("Failed to send stored reports due to error: " + e.Message);
            }
        }