Beispiel #1
0
        /// <summary>
        /// Executes the <c>async</c> HTTP request to Sentry.
        /// </summary>
        /// <returns>
        /// The <see cref="JsonPacket.EventID" /> of the successfully captured JSON packet, or <c>null</c> if it fails.
        /// </returns>
        public async Task <string> RequestAsync()
        {
            using (var s = await this.webRequest.GetRequestStreamAsync())
            {
                if (this.useCompression)
                {
                    await GzipUtil.WriteAsync(this.data.Scrubbed, s);
                }
                else
                {
                    using (var sw = new StreamWriter(s))
                    {
                        await sw.WriteAsync(this.data.Scrubbed);
                    }
                }
            }

            using (var wr = (HttpWebResponse)await this.webRequest.GetResponseAsync())
            {
                using (var responseStream = wr.GetResponseStream())
                {
                    if (responseStream == null)
                    {
                        return(null);
                    }

                    using (var sr = new StreamReader(responseStream))
                    {
                        var content = await sr.ReadToEndAsync();

                        var response = JsonConvert.DeserializeObject <dynamic>(content);
                        return(response.id);
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>Sends the specified packet to Sentry.</summary>
        /// <param name="packet">The packet to send.</param>
        /// <returns>
        /// The <see cref="JsonPacket.EventID" /> of the successfully captured JSON packet, or <c>null</c> if it fails.
        /// </returns>
        protected virtual async Task <string> SendAsync(JsonPacket packet)
        {
            try
            {
                packet = PreparePacket(packet);

                var request = (HttpWebRequest)WebRequest.Create(CurrentDsn.SentryUri);
                request.Timeout          = (int)Timeout.TotalMilliseconds;
                request.ReadWriteTimeout = (int)Timeout.TotalMilliseconds;
                request.Method           = "POST";
                request.Accept           = "application/json";
                request.Headers.Add("X-Sentry-Auth", PacketBuilder.CreateAuthenticationHeader(CurrentDsn));
                request.UserAgent = PacketBuilder.UserAgent;

                if (Compression)
                {
                    request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");
                    request.AutomaticDecompression = DecompressionMethods.Deflate;
                    request.ContentType            = "application/octet-stream";
                }
                else
                {
                    request.ContentType = "application/json; charset=utf-8";
                }

                /*string data = packet.ToString(Formatting.Indented);
                 * Console.WriteLine(data);*/

                string data = packet.ToString(Formatting.None);

                if (LogScrubber != null)
                {
                    data = LogScrubber.Scrub(data);
                }

                // Write the messagebody.
                using (Stream s = await request.GetRequestStreamAsync())
                {
                    if (Compression)
                    {
                        await GzipUtil.WriteAsync(data, s);
                    }
                    else
                    {
                        using (StreamWriter sw = new StreamWriter(s))
                        {
                            await sw.WriteAsync(data);
                        }
                    }
                }

                using (HttpWebResponse wr = (HttpWebResponse)(await request.GetResponseAsync()))
                {
                    using (Stream responseStream = wr.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            return(null);
                        }

                        using (StreamReader sr = new StreamReader(responseStream))
                        {
                            string content = await sr.ReadToEndAsync();

                            var response = JsonConvert.DeserializeObject <dynamic>(content);
                            return(response.id);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(HandleException(ex));
            }
        }