Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Merge pull request #135 from getsentry/feature/improved-logging
Browse files Browse the repository at this point in the history
Improved logging
  • Loading branch information
asbjornu authored Aug 12, 2016
2 parents 6049832 + 5218e48 commit 83344ff
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 83 deletions.
44 changes: 11 additions & 33 deletions src/app/SharpRaven/RavenClient.Net45.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,37 +138,15 @@ public async Task<string> CaptureMessageAsync(SentryMessage message,
/// </returns>
protected virtual async Task<string> SendAsync(JsonPacket packet)
{
string data = null;
HttpWebRequest request = null;

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);
request = CreateWebRequest(packet, out data);

// Write the messagebody.
using (Stream s = await request.GetRequestStreamAsync())
using (var s = await request.GetRequestStreamAsync())
{
if (Compression)
await GzipUtil.WriteAsync(data, s);
Expand All @@ -181,25 +159,25 @@ protected virtual async Task<string> SendAsync(JsonPacket packet)
}
}

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

using (StreamReader sr = new StreamReader(responseStream))
using (var sr = new StreamReader(responseStream))
{
string content = await sr.ReadToEndAsync();
var content = await sr.ReadToEndAsync();
var response = JsonConvert.DeserializeObject<dynamic>(content);
return response.id;
}
}
}
}
catch (Exception ex)
catch (Exception exception)
{
return HandleException(ex);
return HandleException(exception, data, request);
}
}
}
Expand Down
126 changes: 86 additions & 40 deletions src/app/SharpRaven/RavenClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,36 +333,12 @@ protected virtual JsonPacket PreparePacket(JsonPacket packet)
/// </returns>
protected virtual string Send(JsonPacket packet)
{
string data = null;
HttpWebRequest request = null;

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);
request = CreateWebRequest(packet, out data);

// Write the messagebody.
using (var s = request.GetRequestStream())
Expand Down Expand Up @@ -394,15 +370,50 @@ protected virtual string Send(JsonPacket packet)
}
}
}
catch (Exception ex)
catch (Exception exception)
{
return HandleException(ex);
return HandleException(exception, data, request);
}
}


private string HandleException(Exception exception)
private HttpWebRequest CreateWebRequest(JsonPacket packet, out string data)
{
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);*/

data = packet.ToString(Formatting.None);

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

return request;
}


private string HandleException(Exception exception, string data, WebRequest request)
{
string id = null;

try
{
if (ErrorOnCapture != null)
Expand All @@ -411,38 +422,73 @@ private string HandleException(Exception exception)
return null;
}

Console.ForegroundColor = ConsoleColor.Red;
Console.Write("[ERROR] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(exception);
if (exception != null)
WriteError(exception.ToString());

WriteError("Request body:", data);
WriteError("Request headers:", request.Headers.ToString());

var webException = exception as WebException;
if (webException == null || webException.Response == null)
return null;

var response = webException.Response;
id = response.Headers["X-Sentry-ID"];
if (String.IsNullOrWhiteSpace(id))
id = null;

string messageBody;
using (var stream = webException.Response.GetResponseStream())
using (var stream = response.GetResponseStream())
{
if (stream == null)
return null;
return id;

using (var sw = new StreamReader(stream))
{
messageBody = sw.ReadToEnd();
}
}

Console.WriteLine("[MESSAGE BODY] " + messageBody);
WriteError("Response headers:", response.Headers.ToString());
WriteError("Response body:", messageBody);
}
catch (Exception onErrorException)
{
Console.WriteLine(onErrorException);
WriteError(onErrorException.ToString());
}

return null;
return id;
}


private static void WriteError(string error)
{
if (error == null)
return;

Console.ForegroundColor = ConsoleColor.Red;
Console.Write("[ERROR] ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(error);
}


private static void WriteError(string description, string multilineData)
{
if (multilineData == null)
return;

var lines = multilineData.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length <= 0)
return;

WriteError(description);
foreach (var line in lines)
{
WriteError(line);
}
}

private IDictionary<string, string> MergeTags(IDictionary<string, string> tags = null)
{
if (tags == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public void Setup()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);
}

Expand All @@ -81,7 +80,6 @@ public async void CaptureExceptionAsync_CanLogException_If_Send_Fails()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

await this.ravenClient.CaptureExceptionAsync(Helper.GetException());
Expand All @@ -101,7 +99,6 @@ public async void CaptureExceptionAsync_Doesnt_Fail_On_Error_During_Send()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

await this.ravenClient.CaptureExceptionAsync(Helper.GetException());
Expand Down Expand Up @@ -176,7 +173,6 @@ public async void CaptureMessageAsync_CanLogException_If_Send_Fails()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

await this.ravenClient.CaptureMessageAsync("Test message");
Expand All @@ -196,7 +192,6 @@ public async void CaptureMessageAsync_Doesnt_Fail_On_Error_During_Send()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

await this.ravenClient.CaptureMessageAsync("Test message");
Expand Down
5 changes: 0 additions & 5 deletions src/tests/SharpRaven.UnitTests/Integration/CaptureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void Setup()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);
}

Expand All @@ -79,7 +78,6 @@ public void CaptureException_CanLogException_If_Send_Fails()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

try
Expand All @@ -106,7 +104,6 @@ public void CaptureException_Doesnt_Fail_On_Error_During_Send()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

try
Expand Down Expand Up @@ -203,7 +200,6 @@ public void CaptureMessage_CanLogException_If_Send_Fails()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

this.ravenClient.CaptureMessage("Test message");
Expand All @@ -223,7 +219,6 @@ public void CaptureMessage_Doesnt_Fail_On_Error_During_Send()
Helper.PrintInfo("Sentry Uri: " + this.ravenClient.CurrentDsn.SentryUri);
Helper.PrintInfo("Port: " + this.ravenClient.CurrentDsn.Port);
Helper.PrintInfo("Public Key: " + this.ravenClient.CurrentDsn.PublicKey);
Helper.PrintInfo("Private Key: " + this.ravenClient.CurrentDsn.PrivateKey);
Helper.PrintInfo("Project ID: " + this.ravenClient.CurrentDsn.ProjectID);

Assert.DoesNotThrow(() => this.ravenClient.CaptureMessage("Test message"));
Expand Down

0 comments on commit 83344ff

Please sign in to comment.