public Response Send(Event e) { Validate (e); string json = GenerateRequestJSONString (e); // Make POST request WebRequest request = WebRequest.Create(ClusterURL+"/save"); request.ContentType = "text/json"; request.Method = "POST"; // Set JSON data AutoResetEvent autoResetEventStream = new AutoResetEvent(false); IAsyncResult asyncResultStream = request.BeginGetRequestStream(r => autoResetEventStream.Set(), null); // Wait until tit's finished autoResetEventStream.WaitOne(); Stream stream = request.EndGetRequestStream(asyncResultStream) as Stream; StreamWriter streamWriter = new StreamWriter (stream); streamWriter.Write(json); streamWriter.Flush(); // Send and wait AutoResetEvent autoResetEvent = new AutoResetEvent(false); IAsyncResult asyncResult = request.BeginGetResponse(r => autoResetEvent.Set(), null); // Wait until the call is finished autoResetEvent.WaitOne(); HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse; int code = (int)response.StatusCode; if (code != 200) { throw new WebException (code.ToString() +": "+ClusterURL+"/save isn't reachable"); } // Parse JSON body string body; using (var sr = new StreamReader(response.GetResponseStream())) { body = sr.ReadToEnd(); } // Decode JSON string into Response object Response decoded = JsonConvert.DeserializeObject<Response>(body); return decoded; }
public Response SendException(Exception e) { Event ev = new Event(e.Message, e.StackTrace, Event.FatalLogLevel, "", ""); return Send (ev); }
// Request JSON for LogPacker private string GenerateRequestJSONString(Event e) { // We send only one message(Event) per request return "{" + "\"client\":{" + "\"user_id\":\""+e.UserID+"\","+ "\"user_name\":\""+e.UserName+"\","+ "\"url\":\"\","+ "\"environment\":\""+Environment+"\","+ "\"agent\":\""+Agent+"\""+ "},"+ "\"messages\":[{"+ "\"message\":\""+e.Message+"\","+ "\"source\":\""+e.Source+"\","+ "\"line\":0,"+ "\"column\":0,"+ "\"log_level\":"+e.LogLevel.ToString()+","+ "\"tag_name\":\"mobile\""+ "}],"+ "\"cloud_key\":\""+CloudKey+"\""+ "}"; }
private bool Validate(Event e) { if (e.Message == "") { throw new ArgumentException("Message cannot be empty"); } if (e.LogLevel < Event.FatalLogLevel || e.LogLevel > Event.NoticeLogLevel) { throw new ArgumentException("LogLevel is invalid. Valid are: "+Event.FatalLogLevel.ToString()+" - "+Event.NoticeLogLevel.ToString()); } return true; }