Esempio n. 1
0
        public void On(Event <LogEventData> evt)
        {
            _suppressions = _suppressions ?? new EventTypeSuppressions(SuppressionMinutes);
            if (_suppressions.ShouldSuppressAt(evt.EventType, DateTime.UtcNow))
            {
                return;
            }

            var color = EventFormatting.LevelToColor(evt.Data.Level);

            var message = new SlackMessage("[" + evt.Data.Level + "] " + evt.Data.RenderedMessage,
                                           GenerateMessageText(evt),
                                           string.IsNullOrWhiteSpace(Username) ? App.Title : EventFormatting.SubstitutePlaceholders(Username, evt, false),
                                           string.IsNullOrWhiteSpace(IconUrl) ? DefaultIconUrl : IconUrl,
                                           Channel);

            if (_slackApi == null)
            {
                _slackApi = new SlackApi(ProxyServer);
            }

            if (ExcludePropertyInformation)
            {
                _slackApi.SendMessage(WebhookUrl, message);
                return;
            }

            if (IsAlert(evt))
            {
                var resultsUrl  = EventFormatting.SafeGetProperty(evt, "ResultsUrl");
                var resultsText = SlackSyntax.Hyperlink(resultsUrl, "Explore detected results in Seq");
                var results     = new SlackMessageAttachment(color, resultsText);
                message.Attachments.Add(results);

                if (MessageTemplate != null)
                {
                    message.Attachments.Add(new SlackMessageAttachment(color, MessageTemplate, null, true));
                }

                _slackApi.SendMessage(WebhookUrl, message);
                return;
            }

            var special = new SlackMessageAttachment(color);

            special.Fields.Add(new SlackMessageAttachmentField("Level", evt.Data.Level.ToString(), @short: true));
            message.Attachments.Add(special);

            foreach (var key in SpecialProperties)
            {
                if (evt.Data.Properties == null || !evt.Data.Properties.ContainsKey(key))
                {
                    continue;
                }

                var property = evt.Data.Properties[key];
                special.Fields.Add(new SlackMessageAttachmentField(key, property.ToString(), @short: true));
            }

            if (evt.Data.Exception != null)
            {
                message.Attachments.Add(new SlackMessageAttachment(color, SlackSyntax.Preformatted(evt.Data.Exception), "Exception Details", textIsMarkdown: true));
            }

            if (evt.Data.Properties != null && evt.Data.Properties.TryGetValue("StackTrace", out var st) && st is string stackTrace)
            {
                message.Attachments.Add(new SlackMessageAttachment(color, SlackSyntax.Preformatted(stackTrace), "Stack Trace", textIsMarkdown: true));
            }

            var otherProperties = new SlackMessageAttachment(color, "Properties");

            if (evt.Data.Properties != null)
            {
                foreach (var property in evt.Data.Properties)
                {
                    if (SpecialProperties.Contains(property.Key))
                    {
                        continue;
                    }
                    if (property.Key == "StackTrace")
                    {
                        continue;
                    }

                    string value = ConvertPropertyValueToString(property.Value);

                    otherProperties.Fields.Add(new SlackMessageAttachmentField(property.Key, value, @short: false));
                }
            }

            if (otherProperties.Fields.Count != 0)
            {
                message.Attachments.Add(otherProperties);
            }

            _slackApi.SendMessage(WebhookUrl, message);
        }