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

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

            messageTemplate    = Template.Parse(MessageTemplate);
            usernameTemplate   = Template.Parse(Username);
            attachmentTemplate = Template.Parse(Attachment);

            var param = new
            {
                Id         = evt.Data.Id,
                Level      = evt.Data.Level.ToString(),
                Message    = evt.Data.RenderedMessage,
                Exception  = evt.Data.Exception,
                Properties = evt.Data.Properties,
                Timestamp  = evt.Data.LocalTimestamp,
                EventType  = evt.EventType,
            };

            var message = new SlackMessage("[" + evt.Data.Level + "] " + evt.Data.RenderedMessage,
                                           GenerateMessageText(evt, param),
                                           string.IsNullOrWhiteSpace(Username) ? App.Title : usernameTemplate.Render(Hash.FromAnonymousObject(evt.Data)),
                                           string.IsNullOrWhiteSpace(IconUrl) ? DefaultIconUrl : IconUrl,
                                           Channel);

            if (!string.IsNullOrEmpty(Attachment))
            {
                var color = EventFormatting.LevelToColor(evt.Data.Level);

                attachmentTemplate.Render(Hash.FromAnonymousObject(param));

                var attachment = new SlackMessageAttachment(color, attachmentTemplate.Render(Hash.FromAnonymousObject(param)));
                message.Attachments.Add(attachment);
            }

            slackApi.SendMessage(WebhookUrl, message);
        }
Esempio n. 2
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 includedPropertiesArray = string.IsNullOrWhiteSpace(IncludedProperties) ? new string[0] : IncludedProperties.Split(',').Select(x => x.Trim());
            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;
                    }
                    if (includedPropertiesArray.Any() && !includedPropertiesArray.Contains(property.Key))
                    {
                        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);
        }