/// <summary> /// Sends the specified notice to Airbrake. /// </summary> /// <param name="notice">The notice.</param> public void Send(AirbrakeNotice notice) { _log.Debug(f => f("{0}.Send({1})", GetType(), notice)); try { // Create the web request var request = WebRequest.Create(_configuration.ServerUri) as HttpWebRequest; if (request == null) { _log.Fatal(f => f("Couldn't create a request to '{0}'.", _configuration.ServerUri)); 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, request); } catch (Exception exception) { _log.Fatal("An error occurred while trying to send to Airbrake.", exception); } }
public void Maximal_notice_generates_valid_XML() { var error = Activator.CreateInstance<AirbrakeError>(); error.Class = "TestError"; error.Message = "something blew up"; error.Backtrace = new[] { new AirbrakeTraceLine("unknown.cs", 0, "unknown") }; var notice = new AirbrakeNotice { ApiKey = "123456", Error = error, Request = new AirbrakeRequest("http://example.com/", GetType().FullName, "Maximal_notice_generates_valid_XML") { Component = "MyApp.HomeController", CgiData = new[] { new AirbrakeVar("REQUEST_METHOD", "POST") }, Params = new[] { new AirbrakeVar("Form.Key1", "Form.Value1") }, Session = new[] { new AirbrakeVar("UserId", "1") }, Url = "http://example.com/myapp", }, Notifier = new AirbrakeNotifier { Name = "sharpbrake", Version = "2.2", Url = "http://github.com/asbjornu/SharpBrake", }, ServerEnvironment = new AirbrakeServerEnvironment("staging") { ProjectRoot = "/test", }, }; var serializer = new CleanXmlSerializer<AirbrakeNotice>(); var xml = serializer.ToXml(notice); Util.ValidateSchema(xml); }
public void Minimal_notice_with_request_generates_valid_XML() { var error = Activator.CreateInstance<AirbrakeError>(); error.Class = "TestError"; error.Message = "something blew up"; error.Backtrace = new[] { new AirbrakeTraceLine("unknown.cs", 0, "unknown") }; var notice = new AirbrakeNotice { ApiKey = "123456", Error = error, Request = new AirbrakeRequest("http://example.com/", GetType().FullName, "") { Session = new AirbrakeVar[0] }, Notifier = new AirbrakeNotifier { Name = "sharpbrake", Version = "2.2", Url = "http://github.com/asbjornu/SharpBrake" }, ServerEnvironment = new AirbrakeServerEnvironment("staging") { ProjectRoot = "/test", }, }; var serializer = new CleanXmlSerializer<AirbrakeNotice>(); var xml = serializer.ToXml(notice); Util.ValidateSchema(xml); }
public void Notice_missing_error_fails_validation() { var notice = new AirbrakeNotice { ApiKey = "123456", Request = new AirbrakeRequest("http://example.com/", GetType().FullName, "") { Action = "Maximal_notice_generates_valid_XML", }, Notifier = new AirbrakeNotifier { Name = "sharpbrake", Version = "2.2", Url = "http://github.com/asbjornu/SharpBrake" }, ServerEnvironment = new AirbrakeServerEnvironment("staging") { ProjectRoot = "/test", }, }; var serializer = new CleanXmlSerializer<AirbrakeNotice>(); var xml = serializer.ToXml(notice); TestDelegate throwing = () => Util.ValidateSchema(xml); var exception = Assert.Throws<XmlSchemaValidationException>(throwing); Console.WriteLine(exception); Assert.That(exception.Message, Is.StringContaining("notice")); Assert.That(exception.Message, Is.StringContaining("error")); Assert.That(exception.LineNumber, Is.EqualTo(18)); Assert.That(exception.LinePosition, Is.EqualTo(3)); }
private void SetRequestBody(WebRequest request, AirbrakeNotice notice) { var serializer = new CleanXmlSerializer<AirbrakeNotice>(); var xml = serializer.ToXml(notice); _log.Debug(f => f("Sending the following to '{0}':\n{1}", request.RequestUri, xml)); var payload = Encoding.UTF8.GetBytes(xml); request.ContentLength = payload.Length; using (var stream = request.GetRequestStream()) { stream.Write(payload, 0, payload.Length); } }