public static RaygunRequestMessage Build(HttpRequestMessage request, RaygunRequestMessageOptions options) { var message = new RaygunRequestMessage(); options = options ?? new RaygunRequestMessageOptions(); message.HostName = request.RequestUri.Host; message.Url = request.RequestUri.AbsolutePath; message.HttpMethod = request.Method.ToString(); message.IPAddress = GetIPAddress(request); message.Form = ToDictionary(request.GetQueryNameValuePairs(), options.IsFormFieldIgnored); message.QueryString = ToDictionary(request.GetQueryNameValuePairs(), s => false); SetHeaders(message, request, options.IsHeaderIgnored); if (!options.IsRawDataIgnored) { string contentType = null; if (message.Headers != null && message.Headers.Contains("Content-Type")) { contentType = (string)message.Headers["Content-Type"]; } if (contentType == null || CultureInfo.InvariantCulture.CompareInfo.IndexOf(contentType, "application/x-www-form-urlencoded", CompareOptions.IgnoreCase) < 0) { object body; if (request.Properties.TryGetValue(RaygunWebApiDelegatingHandler.RequestBodyKey, out body)) { message.RawData = body.ToString(); } } } return message; }
public void QueryStringTest() { var request = new HttpRequest("test", "http://google.com", "test=test"); var message = new RaygunRequestMessage(request, new List<string>()); Assert.That(message.QueryString, Contains.Item(new KeyValuePair<string, string>("test", "test"))); }
public void FormFields() { var request = CreateWritableRequest(); request.Form.Add("TestFormField1", "FormFieldValue"); request.Form.Add("TestFormField2", "FormFieldValue"); request.Form.Add("TestFormField3", "FormFieldValue"); Assert.AreEqual(3, request.Form.Count); var message = new RaygunRequestMessage(request, new RaygunRequestMessageOptions()); Assert.AreEqual(3, message.Form.Count); Assert.IsTrue(message.Form.Contains("TestFormField1")); Assert.IsTrue(message.Form.Contains("TestFormField2")); Assert.IsTrue(message.Form.Contains("TestFormField3")); }
public static async Task<RaygunRequestMessage> Build(HttpContext context, RaygunRequestMessageOptions options) { var request = context.Request; options = options ?? new RaygunRequestMessageOptions(); var message = new RaygunRequestMessage { HostName = request.Host.Value, Url = request.GetDisplayUrl(), HttpMethod = request.Method, IPAddress = GetIpAddress(context.Connection), Form = await GetForm(options, request), Cookies = GetCookies(options, request), QueryString = GetQueryString(request), RawData = GetRawData(options, request), Headers = GetHeaders(request, options.IsHeaderIgnored) }; return message; }
private RaygunRequestMessage BuildRequestMessage() { RaygunRequestMessage requestMessage = null; HttpContext context = HttpContext.Current; if (context != null) { HttpRequest request = null; try { request = context.Request; } catch (HttpException) { } if (request != null) { requestMessage = new RaygunRequestMessage(request, _requestMessageOptions ?? new RaygunRequestMessageOptions()); } } return requestMessage; }
public void IgnoreCookie_CaseInsensitive() { var request = new HttpRequest("test", "http://google.com", "test=test"); request.Cookies.Add(new HttpCookie("TESTCOOKIE", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie", "CookieValue")); request.Cookies.Add(new HttpCookie("testcookie", "CookieValue")); Assert.AreEqual(3, request.Cookies.Count); var options = new RaygunRequestMessageOptions(Enumerable.Empty<string>(), Enumerable.Empty<string>(), new string[] { "TeStCoOkIe" }, Enumerable.Empty<string>()); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(0, message.Cookies.Count); }
/// <summary> /// Transmits an exception to Raygun.io synchronously specifying a list of string tags associated /// with the message for identification, as well as sending a key-value collection of custom data. /// This uses the version number of the originating assembly. /// </summary> /// <param name="exception">The exception to deliver.</param> /// <param name="tags">A list of strings associated with the message.</param> /// <param name="userCustomData">A key-value collection of custom data that will be added to the payload.</param> /// <param name="userInfo">Information about the user including the identity string.</param> public void Send(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo) { if (CanSend(exception)) { _currentRequestMessage = BuildRequestMessage(); Send(BuildMessage(exception, tags, userCustomData, userInfo, null)); FlagAsSent(exception); } }
public void IgnoreMultipleFormFields() { var request = CreateWritableRequest(); request.Form.Add("TestFormField1", "FormFieldValue"); request.Form.Add("TestFormField2", "FormFieldValue"); request.Form.Add("TestFormField3", "FormFieldValue"); Assert.AreEqual(3, request.Form.Count); var options = new RaygunRequestMessageOptions(new string[] { "TestFormField1", "TestFormField3" }, _empty, _empty, _empty); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(1, message.Form.Count); Assert.IsTrue(message.Form.Contains("TestFormField2")); }
public void IgnoreCookie_Contains() { var request = new HttpRequest("test", "http://google.com", "test=test"); request.Cookies.Add(new HttpCookie("TestCookieTest", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie", "CookieValue")); request.Cookies.Add(new HttpCookie("CookieTest", "CookieValue")); Assert.AreEqual(3, request.Cookies.Count); var options = new RaygunRequestMessageOptions(_empty, _empty, new string[] { "*cookie*" }, _empty); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(0, message.Cookies.Count); }
public void HttpMethodTest() { var message = new RaygunRequestMessage(_defaultRequest, Enumerable.Empty<string>().ToList()); Assert.That(message.HttpMethod, Is.EqualTo("GET")); }
public void HostNameTest() { var message = new RaygunRequestMessage(_defaultRequest, new List<string>()); Assert.That(message.HostName, Is.EqualTo("google.com")); }
/// <summary> /// Transmits an exception to Raygun.io synchronously specifying a list of string tags associated /// with the message for identification, as well as sending a key-value collection of custom data. /// </summary> /// <param name="exception">The exception to deliver.</param> /// <param name="tags">A list of strings associated with the message.</param> /// <param name="userCustomData">A key-value collection of custom data that will be added to the payload.</param> public void Send(Exception exception, IList<string> tags, IDictionary userCustomData) { _currentRequestMessage = BuildRequestMessage(); Send(BuildMessage(exception, tags, userCustomData, null)); }
public IRaygunMessageBuilder SetHttpDetails(RaygunRequestMessage message) { _raygunMessage.Details.Request = message; return this; }
public void IgnoreFormField() { var request = CreateWritableRequest(); request.Form.Add("TestFormField1", "FormFieldValue"); request.Form.Add("TestFormField2", "FormFieldValue"); request.Form.Add("TestFormField3", "FormFieldValue"); Assert.AreEqual(3, request.Form.Count); var options = new RaygunRequestMessageOptions(new string[] { "TestFormField2" }, Enumerable.Empty<string>(), Enumerable.Empty<string>(), Enumerable.Empty<string>()); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(2, message.Form.Count); Assert.IsTrue(message.Form.Contains("TestFormField1")); Assert.IsTrue(message.Form.Contains("TestFormField3")); }
private static void SetHeaders(RaygunRequestMessage message, HttpRequestMessage request, Func<string, bool> ignored) { message.Headers = new Dictionary<string, string>(); foreach (var header in request.Headers.Where(h => !ignored(h.Key))) { message.Headers[header.Key] = string.Join(",", header.Value); } try { if (request.Content.Headers.ContentLength.HasValue && request.Content.Headers.ContentLength.Value > 0) { foreach (var header in request.Content.Headers) { message.Headers[header.Key] = string.Join(",", header.Value); } } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("Error retrieving Headers: {0}", ex.Message); } }
public RaygunAspNetCoreMessageBuilder SetRequestDetails(RaygunRequestMessage message) { _raygunMessage.Details.Request = message; return this; }
/// <summary> /// Transmits an exception to Raygun.io synchronously specifying a list of string tags associated /// with the message for identification, as well as sending a key-value collection of custom data. /// This uses the version number of the originating assembly. /// </summary> /// <param name="exception">The exception to deliver.</param> /// <param name="tags">A list of strings associated with the message.</param> /// <param name="userCustomData">A key-value collection of custom data that will be added to the payload.</param> public void Send(Exception exception, IList<string> tags, IDictionary userCustomData) { if (CanSend(exception)) { _currentRequestMessage = BuildRequestMessage(); Send(BuildMessage(exception, tags, userCustomData)); FlagAsSent(exception); } }
public static RaygunRequestMessage Build(HttpRequest request, RaygunRequestMessageOptions options) { RaygunRequestMessage message = new RaygunRequestMessage(); options = options ?? new RaygunRequestMessageOptions(); try { message.HostName = request.Url.Host; message.Url = request.Url.AbsolutePath; message.HttpMethod = request.RequestType; message.IPAddress = GetIpAddress(request); } catch (Exception e) { System.Diagnostics.Trace.WriteLine("Failed to get basic request info: {0}", e.Message); } // Query string try { message.QueryString = ToDictionary(request.QueryString, null); } catch (Exception e) { if (message.QueryString == null) { message.QueryString = new Dictionary<string, string>() { { "Failed to retrieve", e.Message } }; } } // Headers try { message.Headers = ToDictionary(request.Headers, options.IsHeaderIgnored); message.Headers.Remove("Cookie"); } catch (Exception e) { if (message.Headers == null) { message.Headers = new Dictionary<string, string>() { { "Failed to retrieve", e.Message } }; } } // Form fields try { message.Form = ToDictionary(request.Form, options.IsFormFieldIgnored, true); } catch (Exception e) { if (message.Form == null) { message.Form = new Dictionary<string, string>() { { "Failed to retrieve", e.Message } }; } } // Cookies try { message.Cookies = GetCookies(request.Cookies, options.IsCookieIgnored); } catch (Exception e) { System.Diagnostics.Trace.WriteLine("Failed to get cookies: {0}", e.Message); } // Server variables try { message.Data = ToDictionary(request.ServerVariables, options.IsServerVariableIgnored); message.Data.Remove("ALL_HTTP"); message.Data.Remove("HTTP_COOKIE"); message.Data.Remove("ALL_RAW"); } catch (Exception e) { if (message.Data == null) { message.Data = new Dictionary<string, string>() { { "Failed to retrieve", e.Message } }; } } // Raw data if (!options.IsRawDataIgnored) { try { // Don't send the raw request data at all if the content-type is urlencoded var contentType = request.Headers["Content-Type"]; if (contentType != "text/html" && (contentType == null || CultureInfo.InvariantCulture.CompareInfo.IndexOf(contentType, "application/x-www-form-urlencoded", CompareOptions.IgnoreCase) < 0) && request.RequestType != "GET") { int length = 4096; request.InputStream.Seek(0, SeekOrigin.Begin); string temp = new StreamReader(request.InputStream).ReadToEnd(); // If we made it this far, strip out any values that have been marked as ignored form fields Dictionary<string, string> ignored = GetIgnoredFormValues(request.Form, options.IsFormFieldIgnored); temp = StripIgnoredFormData(temp, ignored); if (length > temp.Length) { length = temp.Length; } message.RawData = temp.Substring(0, length); } } catch (Exception e) { if (message.RawData == null) { message.RawData = "Failed to retrieve raw data: " + e.Message; } } } return message; }
/// <summary> /// Asynchronously transmits an exception to Raygun.io. /// </summary> /// <param name="exception">The exception to deliver.</param> /// <param name="tags">A list of strings associated with the message.</param> /// <param name="userCustomData">A key-value collection of custom data that will be added to the payload.</param> /// <param name="userInfo">Information about the user including the identity string.</param> public void SendInBackground(Exception exception, IList<string> tags, IDictionary userCustomData, RaygunIdentifierMessage userInfo) { if (CanSend(exception)) { // We need to process the HttpRequestMessage on the current thread, // otherwise it will be disposed while we are using it on the other thread. RaygunRequestMessage currentRequestMessage = BuildRequestMessage(); DateTime currentTime = DateTime.UtcNow; ThreadPool.QueueUserWorkItem(c => { try { _currentRequestMessage = currentRequestMessage; Send(BuildMessage(exception, tags, userCustomData, userInfo, currentTime)); } catch (Exception) { // This will swallow any unhandled exceptions unless we explicitly want to throw on error. // Otherwise this can bring the whole process down. if (RaygunSettings.Settings.ThrowOnError) { throw; } } }); FlagAsSent(exception); } }
public void IgnoreFormField_Contains() { var request = CreateWritableRequest(); request.Form.Add("TestFormFieldTest", "FormFieldValue"); request.Form.Add("TestFormField", "FormFieldValue"); request.Form.Add("FormFieldTest", "FormFieldValue"); Assert.AreEqual(3, request.Form.Count); var options = new RaygunRequestMessageOptions(new string[] { "*formfield*" }, Enumerable.Empty<string>(), Enumerable.Empty<string>(), Enumerable.Empty<string>()); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(0, message.Form.Count); }
/// <summary> /// Asynchronously transmits an exception to Raygun.io. /// </summary> /// <param name="exception">The exception to deliver.</param> /// <param name="tags">A list of strings associated with the message.</param> /// <param name="userCustomData">A key-value collection of custom data that will be added to the payload.</param> public void SendInBackground(Exception exception, IList<string> tags, IDictionary userCustomData) { // We need to process the HttpRequestMessage on the current thread, // otherwise it will be disposed while we are using it on the other thread. RaygunRequestMessage currentRequestMessage = BuildRequestMessage(); DateTime? currentTime = DateTime.UtcNow; ThreadPool.QueueUserWorkItem(c => { _currentRequestMessage = currentRequestMessage; Send(BuildMessage(exception, tags, userCustomData, currentTime)); }); }
public void IgnoreFormField_CaseInsensitive() { var request = CreateWritableRequest(); request.Form.Add("TESTFORMFIELD", "FormFieldValue"); Assert.AreEqual(1, request.Form.Count); var options = new RaygunRequestMessageOptions(new string[] { "testformfield" }, Enumerable.Empty<string>(), Enumerable.Empty<string>(), Enumerable.Empty<string>()); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(0, message.Form.Count); }
private int CookieCount(RaygunRequestMessage message, string name) { int count = 0; foreach (Mindscape.Raygun4Net.Messages.RaygunRequestMessage.Cookie cookie in message.Cookies) { if (name.Equals(cookie.Name)) { count++; } } return count; }
public void DuplicateCookies() { var request = new HttpRequest("test", "http://google.com", "test=test"); request.Cookies.Add(new HttpCookie("TestCookie", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie", "CookieValue")); Assert.AreEqual(2, request.Cookies.Count); var message = new RaygunRequestMessage(request, new RaygunRequestMessageOptions()); Assert.AreEqual(2, message.Cookies.Count); Assert.AreEqual(2, CookieCount(message, "TestCookie")); }
public void UrlTest() { var message = new RaygunRequestMessage(_defaultRequest, Enumerable.Empty<string>().ToList()); Assert.That(message.Url, Is.EqualTo("/")); }
public void IgnoreAllCookies() { var request = new HttpRequest("test", "http://google.com", "test=test"); request.Cookies.Add(new HttpCookie("TestCookie1", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie2", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie3", "CookieValue")); Assert.AreEqual(3, request.Cookies.Count); var options = new RaygunRequestMessageOptions(Enumerable.Empty<string>(), Enumerable.Empty<string>(), new string[] { "*" }, Enumerable.Empty<string>()); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(0, message.Cookies.Count); }
public void HttpMethodTest() { var message = new RaygunRequestMessage(_defaultRequest, new List<string>()); Assert.That(message.HttpMethod, Is.EqualTo("GET")); }
public void IgnoreCookie_EndsWith() { var request = new HttpRequest("test", "http://google.com", "test=test"); request.Cookies.Add(new HttpCookie("TestCookieTest", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie", "CookieValue")); request.Cookies.Add(new HttpCookie("CookieTest", "CookieValue")); Assert.AreEqual(3, request.Cookies.Count); var options = new RaygunRequestMessageOptions(Enumerable.Empty<string>(), Enumerable.Empty<string>(), new string[] { "*cookie" }, Enumerable.Empty<string>()); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(2, message.Cookies.Count); Assert.AreEqual(1, CookieCount(message, "TestCookieTest")); Assert.AreEqual(1, CookieCount(message, "CookieTest")); }
public void UrlTest() { var message = new RaygunRequestMessage(_defaultRequest, new List<string>()); Assert.That(message.Url, Is.EqualTo("/")); }
public void IgnoreMultipleCookies() { var request = new HttpRequest("test", "http://google.com", "test=test"); request.Cookies.Add(new HttpCookie("TestCookie1", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie2", "CookieValue")); request.Cookies.Add(new HttpCookie("TestCookie3", "CookieValue")); Assert.AreEqual(3, request.Cookies.Count); var options = new RaygunRequestMessageOptions(_empty, _empty, new string[] { "TestCookie1", "TestCookie3" }, _empty); var message = new RaygunRequestMessage(request, options); Assert.AreEqual(1, message.Cookies.Count); Assert.AreEqual(1, CookieCount(message, "TestCookie2")); }