Exemple #1
0
        /// <summary>
        /// Generates a message context from a trace event message
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="detail">The detail.</param>
        /// <param name="eventType">Type of the event.</param>
        /// <returns>MessageContext.</returns>
        public virtual MessageContext MessageFromString(string message, string detail = null,
                                                        TraceEventType eventType      = TraceEventType.Information)
        {
            try
            {
                if (!eventType.IsValid())
                {
                    return(null);
                }

                //Create default message
                if (string.IsNullOrEmpty(message))
                {
                    message = DefaultMessage();
                }

                var tags = new List <string>();
                // get tags from the stack trace
                tags.AddRange(GetAttributeTags());
                // get user from stack trace
                var attributeUser = GetAttributeUser();

                return(new MessageContext(new Exception($"{message}. {detail}"), tags, user: attributeUser));
            }
            catch (Exception e)
            {
                if (NotAlone()) // if someone else is listening, then trace the error
                {
                    Trace.TraceError("Error on MessageFromString in RaygunTraceListener : {0}", e.Message);
                }
                return(new MessageContext(e));
            }
        }
    /// <summary>
    /// Generates a message context from a trace event.
    /// </summary>
    /// <param name="eventCache">The event cache.</param>
    /// <param name="source">The source.</param>
    /// <param name="eventType">Type of the event.</param>
    /// <param name="id">The identifier.</param>
    /// <param name="message">The message.</param>
    /// <param name="args">The arguments.</param>
    /// <returns>MessageContext.</returns>
    protected virtual MessageContext MessageFromTraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message, params object[] args)
    {
      try
      {
        if (!eventType.IsValid())
          return null;

        var context = new MessageContext(new Exception(message), new List<string>(), new Dictionary<object, object>());

        // get tags from the stack trace
        context.Tags.AddRange(GetAttributeTags());

        if (args != null)
        {
          var localArgs = args.Where(a => a != null).ToList();
          // check the args for custom data
          var custom = localArgs.FirstOrDefault(a => a is IDictionary);
          if (custom != null)
          {
            context.Data = (IDictionary)custom;
            localArgs.Remove(custom);
          }

          // check the args for tags
          var tags = localArgs.FirstOrDefault(a => a is IList<string>);
          if (tags != null)
          {
            context.Tags.AddRange((IList<string>)tags);
            localArgs.Remove(tags);
          }

          // check the args for a custom exception
          var error = localArgs.FirstOrDefault(a => a is Exception);
          if (error != null)
          {
            // use the arg exception for raygun and pass the message as custom data
            context.Exception = (Exception)error;
            context.Data.Add("Message", message);
            localArgs.Remove(error);
          }
          else
          {
            // wrap the trace message as the exception
            context.Exception = new Exception(message);
          }

          // add the rest
          var count = 0;
          foreach (var leftover in localArgs)
            context.Data.Add(String.Format("arg-{0}", count++), leftover);
        }

        return context;
      }
      catch (Exception e)
      {
        if (NotAlone()) // if someone else is listening, then trace the error
          Trace.TraceError("Error on MessageFromTraceEvent in RaygunTraceListener : {0}", e.Message);
        return new MessageContext(e);
      }
    }
    /// <summary>
    /// Generates a message context from a trace event message
    /// </summary>
    /// <param name="message">The message.</param>
    /// <param name="detail">The detail.</param>
    /// <param name="eventType">Type of the event.</param>
    /// <returns>MessageContext.</returns>
    protected virtual MessageContext MessageFromString(string message, string detail = null, TraceEventType eventType = TraceEventType.Information)
    {
      try
      {
        if (!eventType.IsValid())
          return null;

        var tags = new List<string>();
        // get tags from the stack trace
        tags.AddRange(GetAttributeTags());

        return new MessageContext(new Exception(string.Format("{0}. {1}", message, detail)), tags);
      }
      catch (Exception e)
      {
        if (NotAlone()) // if someone else is listening, then trace the error
          Trace.TraceError("Error on MessageFromString in RaygunTraceListener : {0}", e.Message);
        return new MessageContext(e);
      }
    }
Exemple #4
0
        /// <summary>
        /// Generates a message context from a trace event.
        /// </summary>
        /// <param name="eventCache">The event cache.</param>
        /// <param name="source">The source.</param>
        /// <param name="eventType">Type of the event.</param>
        /// <param name="id">The identifier.</param>
        /// <param name="message">The message.</param>
        /// <param name="args">The arguments.</param>
        /// <returns>MessageContext.</returns>
        public virtual MessageContext MessageFromTraceEvent(TraceEventCache eventCache, string source,
                                                            TraceEventType eventType, int id, string message, params object[] args)
        {
            try
            {
                if (!eventType.IsValid())
                {
                    return(null);
                }

                //Create default message
                if (string.IsNullOrEmpty(message))
                {
                    message = DefaultMessage();
                }

                var context = new MessageContext(new Exception(message), new List <string>(), new Dictionary <object, object>(),
                                                 new UserInfo(AppDomain.CurrentDomain.FriendlyName)
                {
                    IsAnonymous = true
                });

                // get tags from the stack trace
                context.Tags.AddRange(GetAttributeTags());
                // get user from the stack trace
                var attributeUser = GetAttributeUser();
                if (attributeUser != null)
                {
                    context.User = attributeUser;
                }

                if (args != null)
                {
                    var localArgs = args.Where(a => a != null).ToList();
                    // check the args for custom data
                    var custom = localArgs.FirstOrDefault(a => a is IDictionary);
                    if (custom != null)
                    {
                        context.Data = (IDictionary)custom;
                        localArgs.Remove(custom);
                    }

                    // check the args for tags
                    var tags = localArgs.FirstOrDefault(a => a is IList <string>);
                    if (tags != null)
                    {
                        context.Tags.AddRange((IList <string>)tags);
                        localArgs.Remove(tags);
                    }

                    // check the args for a custom exception
                    var error = localArgs.FirstOrDefault(a => a is Exception);
                    if (error != null)
                    {
                        // use the arg exception for raygun and pass the message as custom data
                        context.Exception = (Exception)error;
                        context.Data.Add("Message", message);
                        localArgs.Remove(error);
                    }
                    else
                    {
                        // wrap the trace message as the exception
                        context.Exception = new Exception(message);
                    }

                    // check for user information
                    var user = localArgs.FirstOrDefault(a => a.HasProperty("username"));
                    if (user != null)
                    {
                        object username;
                        user.TryGetPropertyValue("username", out username);
                        object userId;
                        user.TryGetPropertyValue("id", out userId);
                        object userEmail;
                        user.TryGetPropertyValue("email", out userEmail);
                        object userFullName;
                        user.TryGetPropertyValue("fullname", out userFullName);
                        object userFirstName;
                        user.TryGetPropertyValue("firstname", out userFirstName);
                        object userIsAnonymous;
                        user.TryGetPropertyValue("isAnonymous", out userIsAnonymous);

                        context.User = new UserInfo(username?.ToString())
                        {
                            Id        = userId?.ToString(),
                            Email     = userEmail?.ToString(),
                            FullName  = userFullName?.ToString(),
                            FirstName = userFirstName?.ToString()
                        };
                    }
                    else
                    {
                        user = localArgs.FirstOrDefault(a => a is IUserInfo);
                        if (user != null)
                        {
                            context.User = (IUserInfo)user;
                        }
                    }

                    var grouping = localArgs.FirstOrDefault(a => a.HasProperty("groupkey"));
                    if (grouping != null)
                    {
                        context.Group = GetGrouping(grouping);
                        localArgs.Remove(grouping);
                    }

                    // add the rest
                    var count = 0;
                    foreach (var leftover in localArgs)
                    {
                        context.Data.Add($"arg-{count++}", leftover);
                    }
                }

                return(context);
            }
            catch (Exception e)
            {
                if (NotAlone()) // if someone else is listening, then trace the error
                {
                    Trace.TraceError("Error on MessageFromTraceEvent in RaygunTraceListener : {0}", e.Message);
                }
                return(new MessageContext(e));
            }
        }