public void SerializedJsonPacket_WithValidData_IsValid() { var exception = TestHelper.GetException(); // TODO: This packet should preferably be "complete", i.e. contain as much information as possible. --asbjornu JsonPacket packet = new JsonPacket("https://*****:*****@app.getsentry.com/1337", exception) { Level = ErrorLevel.Fatal, Tags = new Dictionary<string, string> { { "key1", "value1" }, { "key2", "value2" }, }, Request = new SentryRequest { QueryString = "?a=b&c=d", Data = new { A = true, B = 1, C = "Hello" } } }; JObject jPacket = JObject.Parse(packet.ToString()); JsonSchema schema = SchemaHelper.GetSchema(); jPacket.Validate(schema, (s, e) => Console.WriteLine(e.Message)); Console.WriteLine(jPacket); var valid = jPacket.IsValid(schema); Assert.That(valid, Is.True); }
/// <summary> /// Sends the specified packet to Sentry. /// </summary> /// <param name="packet">The packet to send.</param> /// <param name="dsn">The Data Source Name in Sentry.</param> /// <returns> /// The <see cref="JsonPacket.EventID"/> of the successfully captured JSON packet, or <c>null</c> if it fails. /// </returns> private string Send(JsonPacket packet, Dsn dsn) { packet.Logger = Logger; try { HttpWebRequest request = (HttpWebRequest) WebRequest.Create(dsn.SentryUri); request.Method = "POST"; request.Accept = "application/json"; request.ContentType = "application/json; charset=utf-8"; request.Headers.Add("X-Sentry-Auth", PacketBuilder.CreateAuthenticationHeader(dsn)); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; request.UserAgent = PacketBuilder.UserAgent; // Write the messagebody. using (Stream s = request.GetRequestStream()) { using (StreamWriter sw = new StreamWriter(s)) { // Compress and encode. //string data = Utilities.GzipUtil.CompressEncode(packet.Serialize()); //Console.WriteLine("Writing: " + data); // Write to the JSON script when ready. string data = packet.ToString(); if (LogScrubber != null) data = LogScrubber.Scrub(data); sw.Write(data); } } using (HttpWebResponse wr = (HttpWebResponse) request.GetResponse()) { using (Stream responseStream = wr.GetResponseStream()) { if (responseStream == null) return null; using (StreamReader sr = new StreamReader(responseStream)) { string content = sr.ReadToEnd(); var response = JsonConvert.DeserializeObject<dynamic>(content); return response.id; } } } } catch (WebException e) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("[ERROR] "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(e); if (e.Response != null) { string messageBody; using (Stream stream = e.Response.GetResponseStream()) { if (stream == null) return null; using (StreamReader sw = new StreamReader(stream)) { messageBody = sw.ReadToEnd(); } } Console.WriteLine("[MESSAGE BODY] " + messageBody); } return null; } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("[ERROR] "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(e); return null; } }
/// <summary> /// Gets a <see cref="System.String"/> representation of the <see cref="RequestData"/>. /// </summary> /// <returns> /// A <see cref="System.String"/> representation of the <see cref="RequestData"/>. /// </returns> public override string ToString() { return(this.formatted = this.formatted ?? JsonPacket.ToString(Formatting.Indented)); }
/// <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 string Send(JsonPacket packet) { try { // TODO(dcramer): moving this out of Send makes it easier to test the final // generated packet packet = PreparePacket(packet); var request = (HttpWebRequest)WebRequest.Create(this.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(this.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);*/ var data = packet.ToString(Formatting.None); if (LogScrubber != null) data = LogScrubber.Scrub(data); // Write the messagebody. using (var s = request.GetRequestStream()) { if (Compression) GzipUtil.Write(data, s); else { using (var sw = new StreamWriter(s)) { sw.Write(data); } } } using (var wr = (HttpWebResponse)request.GetResponse()) { using (var responseStream = wr.GetResponseStream()) { if (responseStream == null) return null; using (var sr = new StreamReader(responseStream)) { var content = sr.ReadToEnd(); var response = JsonConvert.DeserializeObject<dynamic>(content); return response.id; } } } } catch (Exception ex) { return HandleException(ex); } }
/// <summary> /// Sends the specified packet to Sentry. /// </summary> /// <param name="packet">The packet to send.</param> /// <param name="dsn">The Data Source Name in Sentry.</param> /// <returns> /// The <see cref="JsonPacket.EventID"/> of the successfully captured JSON packet, or <c>null</c> if it fails. /// </returns> protected virtual string Send(JsonPacket packet, Dsn dsn) { packet.Logger = Logger; try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(dsn.SentryUri); request.Method = "POST"; request.Accept = "application/json"; request.Headers.Add("X-Sentry-Auth", PacketBuilder.CreateAuthenticationHeader(dsn)); ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 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 = request.GetRequestStream()) { if (Compression) GzipUtil.Write(data, s); else { using (StreamWriter sw = new StreamWriter(s)) sw.Write(data); } } using (HttpWebResponse wr = (HttpWebResponse)request.GetResponse()) using (Stream responseStream = wr.GetResponseStream()) { if (responseStream == null) return null; using (StreamReader sr = new StreamReader(responseStream)) { string content = sr.ReadToEnd(); var response = JsonConvert.DeserializeObject<dynamic>(content); return response.id; } } } catch (Exception exception) { Console.ForegroundColor = ConsoleColor.Red; Console.Write("[ERROR] "); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(exception); WebException webException = exception as WebException; if (webException != null && webException.Response != null) { string messageBody; using (Stream stream = webException.Response.GetResponseStream()) { if (stream == null) return null; using (StreamReader sw = new StreamReader(stream)) messageBody = sw.ReadToEnd(); } Console.WriteLine("[MESSAGE BODY] " + messageBody); } } return null; }
/// <summary> /// Sends the specified packet to Sentry. /// </summary> /// <param name="packet">The packet to send.</param> /// <param name="dsn">The Data Source Name in Sentry.</param> /// <returns> /// The <see cref="JsonPacket.EventID"/> of the successfully captured JSON packet, or <c>null</c> if it fails. /// </returns> protected virtual string Send(JsonPacket packet, Dsn dsn) { packet.Logger = Logger; var request = (HttpWebRequest) WebRequest.Create(dsn.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(dsn)); 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 = request.GetRequestStream()) { if (Compression) GzipUtil.Write(data, s); else { using (StreamWriter sw = new StreamWriter(s)) sw.Write(data); } } using (HttpWebResponse wr = (HttpWebResponse) request.GetResponse()) using (Stream responseStream = wr.GetResponseStream()) { if (responseStream == null) return null; using (StreamReader sr = new StreamReader(responseStream)) { string content = sr.ReadToEnd(); var response = JsonConvert.DeserializeObject<dynamic>(content); return response.id; } } }