public void Minimal_notice_generates_valid_XML() { var notice = new HoptoadNotice(); notice.ApiKey = "123456"; notice.Error = new HoptoadError { Class = "TestError", Message = "something blew up", Backtrace = new[] { new TraceLine() { File = "unknown.cs", LineNumber = 0, Method = "unknown" } } }; notice.Notifier = new HoptoadNotifier { Name = "hopsharp", Version = "2.0", Url = "http://github.com/krobertson/hopsharp" }; notice.ServerEnvironment = new HoptoadServerEnvironment { ProjectRoot = "/test", EnvironmentName = "staging" }; var serializer = new CleanXmlSerializer<HoptoadNotice>(); var xml = serializer.ToXml(notice); HoptoadValidator.ValidateSchema(xml); }
public HoptoadNotice Notice(Exception exception) { var notice = new HoptoadNotice { ApiKey = this.Configuration.ApiKey, Error = this.ErrorFromException(exception), Notifier = this.Notifier(), ServerEnvironment = this.ServerEnvironment(), }; return notice; }
public HoptoadNotice Notice(HoptoadError error) { var notice = new HoptoadNotice { ApiKey = this.Configuration.ApiKey, Error = error, Notifier = this.Notifier(), ServerEnvironment = this.ServerEnvironment(), }; return notice; }
public void Can_convert_HoptoadNotice_to_json() { var notice = new HoptoadNotice { ApiKey = "12345678", ErrorMessage = "sdlfds", ErrorClass = "sdflshs", Backtrace = "blah1\npoop2" }; var json = notice.Serialize(); Console.WriteLine(json); Assert.AreEqual("{\"notice\":{\"api_key\":\"12345678\",\"error_class\":\"sdflshs\",\"error_message\":\"sdlfds\",\"environment\":{\"RAILS_ENV\":\"Default [Debug]\"},\"request\":null,\"session\":null,\"backtrace\":[\"blah1\",\"poop2\"]}}", json); }
public void Send(HoptoadNotice notice) { try { // If no API key, get it from the appSettings if (string.IsNullOrEmpty(notice.ApiKey)) { // If none is set, just return... throwing an exception is pointless, since one was already thrown! if (string.IsNullOrEmpty(ConfigurationManager.AppSettings["Hoptoad:ApiKey"])) return; notice.ApiKey = _builder.Configuration.ApiKey; } // Create the web request var request = WebRequest.Create("http://hoptoadapp.com/notifier_api/v2/notices") as HttpWebRequest; if (request == null) return; // Set the basic headers request.ContentType = "text/xml"; request.Accept = "text/xml"; request.KeepAlive = false; // It is important to set the method late... .NET quirk, it will interfere with headers set after request.Method = "POST"; // Go populate the body SetRequestBody(request, notice); // Begin the request, yay async request.BeginGetResponse(RequestCallback, null); } // ReSharper disable EmptyGeneralCatchClause catch // ReSharper restore EmptyGeneralCatchClause { // Since an exception was already thrown, allowing another one to bubble up is pointless // But we should log it or something // TODO this could be better } }
private static void SetRequestBody(WebRequest request, HoptoadNotice notice) { var serializer = new CleanXmlSerializer<HoptoadNotice>(); string xml = serializer.ToXml(notice); byte[] payload = Encoding.UTF8.GetBytes(xml); request.ContentLength = payload.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(payload, 0, payload.Length); stream.Close(); } }