예제 #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;
    }
예제 #2
0
 // 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;
 }
예제 #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));
        }
예제 #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));
              }
            }
              }
        }
예제 #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();
            }
              }
        }
예제 #8
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)
 {
   bool calledFromUnhandled = IsCalledFromApplicationUnhandledExceptionHandler();
   Send(raygunMessage, calledFromUnhandled, false);
 }
예제 #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));
              }
            }
              }
            }
              }
        }
예제 #10
0
    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();
 }
예제 #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;
              }
            }
          }
        }
      }
    }
예제 #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));
 }
예제 #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;
 }
예제 #15
0
 protected bool FilterShouldPreventSend(RaygunMessage raygunMessage)
 {
     return MessageSendFilter != null && !MessageSendFilter(raygunMessage);
 }
예제 #16
0
 /// <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);
 }
예제 #17
0
 /// <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);
 }
예제 #18
0
 public bool ExposeCanSend(RaygunMessage message)
 {
   return CanSend(message);
 }
예제 #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);
              }
            }
              }
        }
예제 #20
0
 public bool ExposeFilterShouldPreventSend(RaygunMessage raygunMessage)
 {
     return FilterShouldPreventSend(raygunMessage);
 }
 public bool ExposeOnSendingMessage(RaygunMessage raygunMessage)
 {
   return OnSendingMessage(raygunMessage);
 }
예제 #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);
 }
예제 #23
0
 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));
       }
     }
   }
 }
예제 #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);
            }
              }
            }
              }
        }
예제 #25
0
 private RaygunMessageBuilder()
 {
     _raygunMessage = new RaygunMessage();
 }
예제 #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;
              }
            }
              }
            }
              }
        }
예제 #27
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())
			{
				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));
					}
				}
			}
		}
예제 #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;
 }
예제 #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));
 }
예제 #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 ();
            }
              }
            }
              }
        }