Example #1
0
    // Returns true if the message can be sent, false if the sending is canceled.
    protected bool OnSendingMessage(RaygunMessage raygunMessage)
    {
      bool result = true;

      if (!_handlingRecursiveErrorSending)
      {
        EventHandler<RaygunSendingMessageEventArgs> handler = SendingMessage;
        if (handler != null)
        {
          RaygunSendingMessageEventArgs args = new RaygunSendingMessageEventArgs(raygunMessage);
          try
          {
            handler(this, args);
          }
          catch (Exception e)
          {
            // Catch and send exceptions that occur in the SendingMessage event handler.
            // Set the _handlingRecursiveErrorSending flag to prevent infinite errors.
            _handlingRecursiveErrorSending = true;
            Send(e);
            _handlingRecursiveErrorSending = false;
          }
          result = !args.Cancel;
        }
      }

      return result;
    }
 // Returns true if the message can be sent, false if the sending is canceled.
 protected bool OnSendingMessage(RaygunMessage raygunMessage)
 {
   bool result = true;
   EventHandler<RaygunSendingMessageEventArgs> handler = SendingMessage;
   if (handler != null)
   {
     RaygunSendingMessageEventArgs args = new RaygunSendingMessageEventArgs(raygunMessage);
     handler(this, args);
     result = !args.Cancel;
   }
   return result;
 }
Example #3
0
        /// <summary>
        /// Emit the provided log event to the sink.
        /// </summary>
        /// <param name="logEvent">The log event to write.</param>
        public void Emit(LogEvent logEvent)
        {
            //Include the log level as a tag.
            var tags = _tags.Concat(new []{logEvent.Level.ToString()}).ToList();

            var properties = logEvent.Properties
                         .Select(pv => new { Name = pv.Key, Value = RaygunPropertyFormatter.Simplify(pv.Value) })
                         .ToDictionary(a => a.Name, b => b.Value);

            // Add the message 
            properties.Add("RenderedLogMessage", logEvent.RenderMessage(_formatProvider));
            properties.Add("LogMessageTemplate", logEvent.MessageTemplate.Text);

            // Create new message
            var raygunMessage = new RaygunMessage
                                {
                                    OccurredOn = logEvent.Timestamp.UtcDateTime
                                };

            // Add exception when available
            if (logEvent.Exception != null)
                raygunMessage.Details.Error = new RaygunErrorMessage(logEvent.Exception);

            // Add user when requested
            if (!String.IsNullOrWhiteSpace(_userNameProperty) &&
                logEvent.Properties.ContainsKey(_userNameProperty) &&
                logEvent.Properties[_userNameProperty] != null)
            {
                raygunMessage.Details.User = new RaygunIdentifierMessage(logEvent.Properties[_userNameProperty].ToString());
            }

            // Add version when requested
            if (!String.IsNullOrWhiteSpace(_applicationVersionProperty) &&
                logEvent.Properties.ContainsKey(_applicationVersionProperty) &&
                logEvent.Properties[_applicationVersionProperty] != null)
            {
                raygunMessage.Details.Version = logEvent.Properties[_applicationVersionProperty].ToString();
            }

            // Build up the rest of the message
            raygunMessage.Details.Environment = new RaygunEnvironmentMessage();
            raygunMessage.Details.Tags = tags;
            raygunMessage.Details.UserCustomData = properties;
            raygunMessage.Details.MachineName = Environment.MachineName;
          
            if (HttpContext.Current != null)
                raygunMessage.Details.Request = new RaygunRequestMessage(HttpContext.Current.Request, null);

            // Submit
            _client.SendInBackground(raygunMessage);

        }
        public void CanNotSendIfExcludingStatusCode_MultipleCodes()
        {
            RaygunSettings.Settings.ExcludeHttpStatusCodesList = "400, 404, 501";

              RaygunMessage message = new RaygunMessage
              {
            Details = new RaygunMessageDetails
            {
              Response = new RaygunResponseMessage
              {
            StatusCode = 404
              }
            }
              };

              Assert.IsFalse(_client.ExposeCanSend(message));
        }
        public void CanSendIfMessageIsNull()
        {
            RaygunSettings.Settings.ExcludeHttpStatusCodesList = null;

              // Null message
              Assert.IsTrue(_client.ExposeCanSend(null));

              // Null message details
              Assert.IsTrue(_client.ExposeCanSend(new RaygunMessage()));

              RaygunMessage message = new RaygunMessage
              {
            Details = new RaygunMessageDetails()
              };

              // Null message response

              Assert.IsTrue(_client.ExposeCanSend(message));
        }
Example #6
0
        /// <summary>
        /// Posts a RaygunMessage to the Raygun.io api endpoint.
        /// </summary>
        /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
        /// set to a valid DateTime and as much of the Details property as is available.</param>
        public void Send(RaygunMessage raygunMessage)
        {
            if (ValidateApiKey())
              {
            if (HasInternetConnection)
            {
              using (var client = new WebClient())
              {
            client.Headers.Add("X-ApiKey", _apiKey);
            client.Encoding = System.Text.Encoding.UTF8;

            try
            {
              var message = SimpleJson.SerializeObject(raygunMessage);
              client.UploadString(RaygunSettings.Settings.ApiEndpoint, message);
              System.Diagnostics.Debug.WriteLine("Sending message to Raygun.io");
            }
            catch (Exception ex)
            {
              System.Diagnostics.Debug.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));
              try
              {
                SaveMessage(SimpleJson.SerializeObject(raygunMessage));
                System.Diagnostics.Debug.WriteLine("Exception has been saved to the device to try again later.");
              }
              catch (Exception e)
              {
                System.Diagnostics.Debug.WriteLine(string.Format("Error saving Exception to device {0}", e.Message));
              }
            }
              }
            }
            else
            {
              try
              {
            var message = SimpleJson.SerializeObject(raygunMessage);
            SaveMessage(message);
              }
              catch (Exception ex)
              {
            System.Diagnostics.Debug.WriteLine(string.Format("Error saving Exception to device {0}", ex.Message));
              }
            }
              }
        }
Example #7
0
        /// <summary>
        /// Posts a RaygunMessage to the Raygun.io api endpoint.
        /// </summary>
        /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
        /// set to a valid DateTime and as much of the Details property as is available.</param>
        public override void Send(RaygunMessage raygunMessage)
        {
            bool canSend = OnSendingMessage(raygunMessage);
              if (canSend)
              {
            string message = null;
            try
            {
              message = SimpleJson.SerializeObject(raygunMessage);
            }
            catch (Exception ex)
            {
              System.Diagnostics.Debug.WriteLine(string.Format("Error serializing exception {0}", ex.Message));

              if (RaygunSettings.Settings.ThrowOnError)
              {
            throw;
              }
            }

            if (message != null)
            {
              try
              {
            Send(message);
              }
              catch (Exception ex)
              {
            SaveMessage(message);
            System.Diagnostics.Debug.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));

            if (RaygunSettings.Settings.ThrowOnError)
            {
              throw;
            }
              }

              SendStoredMessages();
            }
              }
        }
 /// <summary>
 /// Posts a RaygunMessage to the Raygun.io api endpoint.
 /// </summary>
 /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
 /// set to a valid DateTime and as much of the Details property as is available.</param>
 public void Send(RaygunMessage raygunMessage)
 {
   bool calledFromUnhandled = IsCalledFromApplicationUnhandledExceptionHandler();
   Send(raygunMessage, calledFromUnhandled, false);
 }
Example #9
0
        /// <summary>
        /// Posts a RaygunMessage to the Raygun.io api endpoint.
        /// </summary>
        /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
        /// set to a valid DateTime and as much of the Details property as is available.</param>
        public override void Send(RaygunMessage raygunMessage)
        {
            if (ValidateApiKey())
              {
            bool canSend = OnSendingMessage(raygunMessage);
            if (canSend)
            {
              string message = null;
              try
              {
            message = SimpleJson.SerializeObject(raygunMessage);
              }
              catch (Exception ex)
              {
            System.Diagnostics.Debug.WriteLine(string.Format("Error Serializing Exception {0}", ex.Message));
              }

              if (!String.IsNullOrWhiteSpace(message))
              {
            using (var client = new WebClient())
            {
              client.Headers.Add("X-ApiKey", _apiKey);
              client.Headers.Add("content-type", "application/json; charset=utf-8");
              client.Encoding = System.Text.Encoding.UTF8;

              try
              {
                client.UploadString(RaygunSettings.Settings.ApiEndpoint, message);
              }
              catch (Exception ex)
              {
                System.Diagnostics.Debug.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));
              }
            }
              }
            }
              }
        }
    private async Task SendOrSave(RaygunMessage raygunMessage)
    {
      if (ValidateApiKey())
      {
        bool canSend = OnSendingMessage(raygunMessage);
        if (canSend)
        {
          try
          {
            string message = SimpleJson.SerializeObject(raygunMessage);

            if (InternetAvailable())
            {
              await SendMessage(message);
            }
            else
            {
              await SaveMessage(message);
            }
          }
          catch (Exception ex)
          {
            Debug.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));
          }
        }
      }
    }
 protected RaygunMessageBuilderBase()
 {
   _raygunMessage = new RaygunMessage();
 }
Example #12
0
    /// <summary>
    /// Posts a RaygunMessage to the Raygun api endpoint.
    /// </summary>
    /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
    /// set to a valid DateTime and as much of the Details property as is available.</param>
    public async Task Send(RaygunMessage raygunMessage)
    {
      if (ValidateApiKey())
      {
        bool canSend = OnSendingMessage(raygunMessage) && CanSend(raygunMessage);
        if (canSend)
        {
          using (var client = new HttpClient())
          {
            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, _settings.ApiEndpoint);

            requestMessage.Headers.Add("X-ApiKey", _apiKey);

            try
            {
              var message = SimpleJson.SerializeObject(raygunMessage);
              requestMessage.Content = new StringContent(message, Encoding.UTF8, "application/json");
              var result = await client.SendAsync(requestMessage);
              if(!result.IsSuccessStatusCode)
              {
                Debug.WriteLine($"Error Logging Exception to Raygun {result.ReasonPhrase}");

                if (_settings.ThrowOnError)
                {
                  throw new Exception("Could not log to Raygun");
                }
              }
            }
            catch (Exception ex)
            {
              Debug.WriteLine(string.Format("Error Logging Exception to Raygun {0}", ex.Message));

              if (_settings.ThrowOnError)
              {
                throw;
              }
            }
          }
        }
      }
    }
Example #13
0
 /// <summary>
 /// Asynchronously transmits a message to Raygun.
 /// </summary>
 /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
 /// set to a valid DateTime and as much of the Details property as is available.</param>
 public Task SendInBackground(RaygunMessage raygunMessage)
 {
   return Task.Run(() => Send(raygunMessage));
 }
Example #14
0
 protected async Task<string> OnCustomGroupingKey(Exception exception, RaygunMessage message)
 {
   string result = null;
   if (!_handlingRecursiveGrouping)
   {
     var handler = CustomGroupingKey;
     if (handler != null)
     {
       var args = new RaygunCustomGroupingKeyEventArgs(exception, message);
       try
       {
         handler(this, args);
       }
       catch (Exception e)
       {
         _handlingRecursiveGrouping = true;
         await SendAsync(e, null, null, null);
         _handlingRecursiveGrouping = false;
       }
       result = args.CustomGroupingKey;
     }
   }
   return result;
 }
Example #15
0
 protected bool FilterShouldPreventSend(RaygunMessage raygunMessage)
 {
     return MessageSendFilter != null && !MessageSendFilter(raygunMessage);
 }
 /// <summary>
 /// Asynchronously sends a RaygunMessage to the Raygun.io api endpoint.
 /// It is best to call this method within a try/catch block.
 /// If the application is crashing due to an unhandled exception, use the synchronous methods instead.
 /// </summary>
 /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
 /// set to a valid DateTime and as much of the Details property as is available.</param>
 public async Task SendAsync(RaygunMessage raygunMessage)
 {
   await SendOrSave(raygunMessage);
 }
 /// <summary>
 /// Sends a RaygunMessage immediately to the Raygun.io endpoint.
 /// </summary>
 /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
 /// set to a valid DateTime and as much of the Details property as is available.</param>
 public void Send(RaygunMessage raygunMessage)
 {
   SendOrSave(raygunMessage).Wait(3000);
 }
 public bool ExposeCanSend(RaygunMessage message)
 {
   return CanSend(message);
 }
Example #19
0
        /// <summary>
        /// Posts a RaygunMessage to the Raygun.io api endpoint.
        /// </summary>
        /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
        /// set to a valid DateTime and as much of the Details property as is available.</param>
        public void Send(RaygunMessage raygunMessage)
        {
            if (ValidateApiKey())
              {
            bool canSend = OnSendingMessage(raygunMessage);
            if (canSend)
            {
              string message = null;

              try
              {
            message = SimpleJson.SerializeObject(raygunMessage);
              }
              catch (Exception ex)
              {
            System.Diagnostics.Debug.WriteLine(string.Format("Error serializing raygun message: {0}", ex.Message));
              }

              if (message != null)
              {
            SendMessage(message);
              }
            }
              }
        }
Example #20
0
 public bool ExposeFilterShouldPreventSend(RaygunMessage raygunMessage)
 {
     return FilterShouldPreventSend(raygunMessage);
 }
 public bool ExposeOnSendingMessage(RaygunMessage raygunMessage)
 {
   return OnSendingMessage(raygunMessage);
 }
Example #22
0
 /// <summary>
 /// Posts a RaygunMessage to the Raygun.io api endpoint.
 /// </summary>
 /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
 /// set to a valid DateTime and as much of the Details property as is available.</param>
 public override void Send(RaygunMessage raygunMessage)
 {
     Send (raygunMessage, SynchronousTimeout);
 }
 private void Send(RaygunMessage raygunMessage, bool wait, bool exit)
 {
   if (ValidateApiKey() && !_exit)
   {
     bool canSend = OnSendingMessage(raygunMessage);
     if (canSend)
     {
       try
       {
         string message = SimpleJson.SerializeObject(raygunMessage);
         if (NetworkInterface.NetworkInterfaceType != NetworkInterfaceType.None)
         {
           SendMessage(message, wait, exit);
         }
         else
         {
           SaveMessage(message);
         }
       }
       catch (Exception ex)
       {
         Debug.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));
       }
     }
   }
 }
Example #24
0
        private void Send(RaygunMessage raygunMessage, int timeout)
        {
            if (ValidateApiKey())
              {
            bool canSend = OnSendingMessage(raygunMessage);
            if (canSend)
            {
              string message = null;
              try
              {
            message = SimpleJson.SerializeObject(raygunMessage);
              }
              catch (Exception ex)
              {
            System.Diagnostics.Debug.WriteLine (string.Format ("Error serializing message {0}", ex.Message));
              }

              if (message != null)
              {
            try
            {
              SaveMessage(message);
            }
            catch (Exception ex)
            {
              System.Diagnostics.Debug.WriteLine (string.Format ("Error saving Exception to device {0}", ex.Message));
              if (HasInternetConnection)
              {
                SendMessage(message, timeout);
              }
            }

            // In the case of sending messages during a crash, only send stored messages if there are 2 or less.
            // This is to prevent keeping the app open for a long time while it crashes.
            if (HasInternetConnection && GetStoredMessageCount() <= 2)
            {
              SendStoredMessages(timeout);
            }
              }
            }
              }
        }
 private RaygunMessageBuilder()
 {
     _raygunMessage = new RaygunMessage();
 }
Example #26
0
        /// <summary>
        /// Posts a RaygunMessage to the Raygun.io api endpoint.
        /// </summary>
        /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
        /// set to a valid DateTime and as much of the Details property as is available.</param>
        public override void Send(RaygunMessage raygunMessage)
        {
            if (ValidateApiKey())
              {
            bool canSend = OnSendingMessage(raygunMessage) && CanSend(raygunMessage);
            if (canSend)
            {
              using (var client = new WebClient())
              {
            client.Headers.Add("X-ApiKey", _apiKey);
            client.Headers.Add("content-type", "application/json; charset=utf-8");
            client.Encoding = System.Text.Encoding.UTF8;

            if (WebRequest.DefaultWebProxy != null)
            {
              Uri proxyUri = WebRequest.DefaultWebProxy.GetProxy(new Uri(RaygunSettings.Settings.ApiEndpoint.ToString()));

              if (proxyUri != null && proxyUri.AbsoluteUri != RaygunSettings.Settings.ApiEndpoint.ToString())
              {
                client.Proxy = new WebProxy(proxyUri, false);

                if (ProxyCredentials == null)
                {
                  client.UseDefaultCredentials = true;
                  client.Proxy.Credentials = CredentialCache.DefaultCredentials;
                }
                else
                {
                  client.UseDefaultCredentials = false;
                  client.Proxy.Credentials = ProxyCredentials;
                }
              }
            }

            try
            {
              var message = SimpleJson.SerializeObject(raygunMessage);
              client.UploadString(RaygunSettings.Settings.ApiEndpoint, message);
            }
            catch (Exception ex)
            {
              System.Diagnostics.Trace.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));

              if (RaygunSettings.Settings.ThrowOnError)
              {
                throw;
              }
            }
              }
            }
              }
        }
		/// <summary>
		/// Posts a RaygunMessage to the Raygun.io api endpoint.
		/// </summary>
		/// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
		/// set to a valid DateTime and as much of the Details property as is available.</param>
		public void Send(RaygunMessage raygunMessage)
		{
			if (ValidateApiKey())
			{
				using (var client = new WebClient())
				{
					client.Headers.Add("X-ApiKey", _apiKey);
					client.Encoding = System.Text.Encoding.UTF8;
					
					try
					{            
						var message = SimpleJson.SerializeObject(raygunMessage);
						client.UploadString(RaygunSettings.Settings.ApiEndpoint, message);
					}
					catch (Exception ex)
					{
						Console.WriteLine(string.Format("Error Logging Exception to Raygun.io {0}", ex.Message));
					}
				}
			}
		}
Example #28
0
 protected bool CanSend(RaygunMessage message)
 {
     if (message != null && message.Details != null && message.Details.Response != null)
       {
     return !RaygunSettings.Settings.ExcludedStatusCodes.Contains(message.Details.Response.StatusCode);
       }
       return true;
 }
Example #29
0
 /// <summary>
 /// Asynchronously transmits a message to Raygun.io.
 /// </summary>
 /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
 /// set to a valid DateTime and as much of the Details property as is available.</param>
 public void SendInBackground(RaygunMessage raygunMessage)
 {
     ThreadPool.QueueUserWorkItem(c => Send(raygunMessage));
 }
Example #30
0
        /// <summary>
        /// Posts a RaygunMessage to the Raygun.io api endpoint.
        /// </summary>
        /// <param name="raygunMessage">The RaygunMessage to send. This needs its OccurredOn property
        /// set to a valid DateTime and as much of the Details property as is available.</param>
        public override void Send(RaygunMessage raygunMessage)
        {
            if (ValidateApiKey())
              {
            bool canSend = OnSendingMessage(raygunMessage);
            if (canSend)
            {
              string message = null;
              try
              {
            message = SimpleJson.SerializeObject(raygunMessage);
              }
              catch (Exception ex)
              {
            System.Diagnostics.Debug.WriteLine (string.Format ("Error serializing message {0}", ex.Message));
              }

              if (message != null)
              {
            try
            {
              SaveMessage (message);
            }
            catch (Exception ex)
            {
              System.Diagnostics.Debug.WriteLine (string.Format ("Error saving Exception to device {0}", ex.Message));
              if (HasInternetConnection)
              {
                SendMessage (message);
              }
            }

            if (HasInternetConnection)
            {
              SendStoredMessages ();
            }
              }
            }
              }
        }