示例#1
0
        /// <summary>
        /// Method used to bind to the CustomGroupingKey event of the RaygunClient object
        /// </summary>
        /// <param name="sender">the Raygun client object firing the event</param>
        /// <param name="e">The custom group arguments of the RaygunClient message</param>
        /// <param name="group">The message group that contains the data to tell the Raygun client how to group the message</param>
        private void HandleGrouping(object sender, RaygunCustomGroupingKeyEventArgs e, IMessageGroup group)
        {
            //Only override the grouping if specified
            if (!String.IsNullOrEmpty(group.GroupKey))
            {
                e.CustomGroupingKey = group.GroupKey;
            }

            OnGrouping?.Invoke(sender, e, group);
        }
示例#2
0
        private void OnCustomGroupingKey(object sender, RaygunCustomGroupingKeyEventArgs e)
        {
            if (e?.Message?.Details != null)
            {
                var details = e.Message.Details;

                details.Client = new RaygunClientMessage
                {
                    Name      = "RaygunSerilogSink",
                    Version   = new AssemblyName(this.GetType().Assembly.FullName).Version.ToString(),
                    ClientUrl = "https://github.com/serilog/serilog-sinks-raygun"
                };

                if (details.UserCustomData is Dictionary <string, LogEventPropertyValue> properties)
                {
                    // If an Exception has not been provided, then use the log message/template to fill in the details and attach the current execution stack
                    if (e.Exception is NullException nullException)
                    {
                        details.Error = new RaygunErrorMessage
                        {
                            ClassName  = properties[LogMessageTemplateProperty].AsString(),
                            Message    = properties[RenderedLogMessageProperty].AsString(),
                            StackTrace = RaygunErrorMessageBuilder.BuildStackTrace(nullException.CodeExecutionStackTrace)
                        };
                    }

                    if (properties.TryGetValue(OccurredProperty, out var occurredOnPropertyValue) &&
                        occurredOnPropertyValue is ScalarValue occurredOnScalar &&
                        occurredOnScalar.Value is DateTime occurredOn)
                    {
                        e.Message.OccurredOn = occurredOn;

                        properties.Remove(OccurredProperty);
                    }

                    // Add user information if provided
                    if (!string.IsNullOrWhiteSpace(_userInfoProperty) &&
                        properties.TryGetValue(_userInfoProperty, out var userInfoPropertyValue) &&
                        userInfoPropertyValue != null)
                    {
                        switch (userInfoPropertyValue)
                        {
                        case StructureValue userInfoStructure:
                            details.User = BuildUserInformationFromStructureValue(userInfoStructure);
                            break;

                        case ScalarValue userInfoScalar when userInfoScalar.Value is string userInfo:
                            details.User = ParseUserInformation(userInfo);
                            break;
                        }

                        if (details.User != null)
                        {
                            details.UserCustomData.Remove(_userInfoProperty);
                        }
                    }

                    // If user information is not set, then use the user-name if provided
                    if (details.User == null &&
                        !string.IsNullOrWhiteSpace(_userNameProperty) &&
                        properties.ContainsKey(_userNameProperty) &&
                        properties[_userNameProperty] != null)
                    {
                        details.User = new RaygunIdentifierMessage(properties[_userNameProperty].AsString());

                        properties.Remove(_userNameProperty);
                    }

                    // Add version if provided
                    if (!string.IsNullOrWhiteSpace(_applicationVersionProperty) &&
                        properties.ContainsKey(_applicationVersionProperty) &&
                        properties[_applicationVersionProperty] != null)
                    {
                        details.Version = properties[_applicationVersionProperty].AsString();

                        properties.Remove(_applicationVersionProperty);
                    }

                    // Add the custom group key if provided
                    if (properties.TryGetValue(_groupKeyProperty, out var customKey))
                    {
                        details.GroupingKey = customKey.AsString();

                        properties.Remove(_groupKeyProperty);
                    }

#if NETSTANDARD2_0
                    // Add Http request/response messages if present and not already set
                    if (details.Request == null &&
                        properties.TryGetValue(RaygunClientHttpEnricher.RaygunRequestMessagePropertyName, out var requestMessageProperty) &&
                        requestMessageProperty is StructureValue requestMessageValue)
                    {
                        details.Request = BuildRequestMessageFromStructureValue(requestMessageValue);
                        properties.Remove(RaygunClientHttpEnricher.RaygunRequestMessagePropertyName);
                    }

                    if (details.Response == null &&
                        properties.TryGetValue(RaygunClientHttpEnricher.RaygunResponseMessagePropertyName, out var responseMessageProperty) &&
                        responseMessageProperty is StructureValue responseMessageValue)
                    {
                        details.Response = BuildResponseMessageFromStructureValue(responseMessageValue);
                        properties.Remove(RaygunClientHttpEnricher.RaygunResponseMessagePropertyName);
                    }
#endif

                    // Simplify the remaining properties to be used as user-custom-data
                    details.UserCustomData = properties
                                             .Select(pv => new { Name = pv.Key, Value = RaygunPropertyFormatter.Simplify(pv.Value) })
                                             .ToDictionary(a => a.Name, b => b.Value);
                }
            }
        }