예제 #1
0
        private string GenerateMessageText(Event <LogEventData> evt)
        {
            var seqUrl = Host.ListenUris.FirstOrDefault();

            if (IsAlert(evt))
            {
                var dashboardUrl   = EventFormatting.SafeGetProperty(evt, "DashboardUrl");
                var condition      = EventFormatting.SafeGetProperty(evt, "Condition", raw: true);
                var dashboardTitle = EventFormatting.SafeGetProperty(evt, "DashboardTitle");
                var chartTitle     = EventFormatting.SafeGetProperty(evt, "ChartTitle");
                var ownerNamespace = "";
                if (evt.Data.Properties.TryGetValue("OwnerUsername", out var ownerUsernameProperty) && ownerUsernameProperty is string ownerUsername)
                {
                    if (!string.IsNullOrEmpty(ownerUsername))
                    {
                        ownerNamespace = SlackSyntax.Escape(ownerUsername) + "/";
                    }
                }
                return($"Alert condition {SlackSyntax.Code(condition)} detected on {SlackSyntax.Hyperlink(dashboardUrl, $"{ownerNamespace}{dashboardTitle}/{chartTitle}")}.");
            }

            var messageTemplateToUse = string.IsNullOrWhiteSpace(MessageTemplate) ? "[RenderedMessage]" : MessageTemplate;
            var message = EventFormatting.SubstitutePlaceholders(messageTemplateToUse, evt);
            var link    = SlackSyntax.Hyperlink($"{seqUrl?.TrimEnd('/')}/#/events?filter=@Id%20%3D%3D%20%22{evt.Id}%22&show=expanded", "View this event in Seq");

            return($"{message} ({link})");
        }
예제 #2
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);
        }
예제 #3
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);
        }