-
-
Save sbrl/4724e8ca1ad6a8bc053ff202d7cf7125 to your computer and use it in GitHub Desktop.
[Sandpiper C# Client] A C# client for Sandpiper - a simple exception reporting and storage system.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Text; | |
namespace SBRL.Utilities | |
{ | |
/// <summary> | |
/// A C# client for Sandpiper - a simple exception reporting and storage system. | |
/// </summary> | |
/// <license>MPL-2.0</license> | |
/// <changelog> | |
/// v0.1 - 15th March 2018: | |
/// - Created this class! | |
/// </changelog> | |
public class SandpiperClient | |
{ | |
public readonly string ServerUri; | |
public readonly string PlaceId; | |
public readonly string ProgramVersion; | |
public SandpiperClient(string serverUri, string placeId, string programVersion) | |
{ | |
ServerUri = serverUri; | |
PlaceId = placeId; | |
ProgramVersion = programVersion; | |
} | |
public void ReportException(Exception error, string details = "") | |
{ | |
string reportUri = ServerUri; | |
Dictionary<string, string> queryParams = new Dictionary<string, string>(); | |
queryParams.Add("place_id", PlaceId); | |
queryParams.Add("summary", error.Message); | |
queryParams.Add("version", ProgramVersion); | |
if(details.Length > 0) | |
queryParams.Add("details", details); | |
reportUri += "?" + BuildQueryString(queryParams); | |
WebClient client = new WebClient(); | |
client.Headers[HttpRequestHeader.ContentType] = "text/plain"; | |
client.UploadData(reportUri, "PUT", Encoding.UTF8.GetBytes(error.ToString())); | |
} | |
private string BuildQueryString(Dictionary<string, string> paramDictionary) | |
{ | |
StringBuilder result = new StringBuilder(); | |
int i = 0; | |
foreach (KeyValuePair<string, string> nextParam in paramDictionary) { | |
if (i > 0) result.Append("&"); | |
result.Append($"{nextParam.Key}={Uri.EscapeDataString(nextParam.Value)}"); | |
i++; | |
} | |
return result.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment