示例#1
0
        private O365MessageCard BuildBody(Event <LogEventData> evt)
        {
            // Build action
            var url = BaseUrl;

            if (string.IsNullOrWhiteSpace(url))
            {
                url = Host.BaseUri;
            }

            var openTitle = "Open Seq Event";
            var openUrl   = $"{url}#/events?filter=@Id%20%3D%3D%20%22{evt.Id}%22&show=expanded";

            if (IsAlert(evt))
            {
                openTitle = "Open Seq Alert";
                openUrl   = SafeGetProperty(evt, "ResultsUrl");
            }

            O365ConnectorCardOpenUri action = new O365ConnectorCardOpenUri()
            {
                Name    = openTitle,
                Type    = "OpenUri", //Failure to provide this will cause a 400 badrequest
                Targets = new[]
                {
                    new O365ConnectorCardOpenUriTarget {
                        Uri = openUrl,
                        Os  = "default" //Failure to provide this will cause a 400 badrequest
                    }
                }
            };

            // Build message
            var msg = evt.Data.RenderedMessage;

            if (msg.Length > 1000)
            {
                msg = msg.EscapeMarkdown().Substring(0, 1000);
            }

            var color = Color;

            if (string.IsNullOrWhiteSpace(color))
            {
                color = _levelColorMap[evt.Data.Level];
            }

            O365MessageCard body = new O365MessageCard
            {
                Title           = evt.Data.Level.ToString().EscapeMarkdown(),
                ThemeColor      = color,
                Text            = msg,
                PotentialAction = new[]
                {
                    action
                }
            };

            // Build sections
            var sections = new List <O365ConnectorCardSection>();

            if (!ExcludeProperties && evt.Data.Properties != null)
            {
                var jsonSerializedProperties = JsonSerializedProperties?.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList() ?? new List <string>();
                var jsonSettings             = new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore,
                    Formatting        = JsonSerializedPropertiesAsIndented ? Formatting.Indented : Formatting.None
                };

                var facts = evt.Data.Properties
                            .Where(i => i.Value != null)
                            .Select(i => new O365ConnectorCardFact
                {
                    Name  = i.Key,
                    Value = (jsonSerializedProperties.Contains(i.Key) ? JsonConvert.SerializeObject(i.Value, jsonSettings) : i.Value.ToString()).EscapeMarkdown()
                })
                            .ToArray();

                if (facts.Any())
                {
                    sections.Add(new O365ConnectorCardSection {
                        Facts = facts
                    });
                }
            }

            if (!string.IsNullOrWhiteSpace(evt.Data.Exception))
            {
                sections.Add(new O365ConnectorCardSection {
                    Title = "Exception", Text = evt.Data.Exception.EscapeMarkdown()
                });
            }

            body.Sections = sections.ToArray();

            return(body);
        }
示例#2
0
        private O365MessageCard BuildBody(Event <LogEventData> evt)
        {
            // Build action
            var url = BaseUrl;

            if (string.IsNullOrWhiteSpace(url))
            {
                url = Host.BaseUri;
            }

            var(openTitle, openUrl) = SeqEvents.GetOpenLink(url, evt);
            var action = new O365ConnectorCardOpenUri
            {
                Name    = openTitle,
                Type    = "OpenUri", //Failure to provide this will cause a 400 badrequest
                Targets = new[]
                {
                    new O365ConnectorCardOpenUriTarget
                    {
                        Uri = openUrl,
                        Os  = "default" //Failure to provide this will cause a 400 badrequest
                    }
                }
            };

            // Build message
            var msg = evt.Data.RenderedMessage;

            if (msg.Length > 1000)
            {
                msg = msg.EscapeMarkdown().Substring(0, 1000);
            }

            var color = Color;

            if (string.IsNullOrWhiteSpace(color))
            {
                color = LevelColorMap[evt.Data.Level];
            }

            var body = new O365MessageCard
            {
                Title           = evt.Data.Level.ToString().EscapeMarkdown(),
                ThemeColor      = color,
                Text            = msg,
                PotentialAction = new O365ConnectorCardActionBase[]
                {
                    action
                }
            };

            // Build sections
            var sections = new List <O365ConnectorCardSection>();
            var config   = new PropertyConfig
            {
                ExcludedProperties                 = ExcludedProperties?.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList() ?? new List <string>(),
                JsonSerializedProperties           = JsonSerializedProperties?.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList() ?? new List <string>(),
                JsonSerializedPropertiesAsIndented = JsonSerializedPropertiesAsIndented
            };

            if (!config.ExcludedProperties.Contains(ExcludeAllPropertyName))
            {
                var properties = SeqEvents.GetProperties(evt, config);

                var facts = properties.Where(x => !string.IsNullOrEmpty(x.Value)).Select(x => new O365ConnectorCardFact
                {
                    Name  = x.Key,
                    Value = x.Value
                }).ToArray();

                if (facts.Any())
                {
                    sections.Add(new O365ConnectorCardSection {
                        Facts = facts
                    });
                }
            }

            if (!string.IsNullOrWhiteSpace(evt.Data.Exception))
            {
                sections.Add(new O365ConnectorCardSection {
                    Title = "Exception", Text = evt.Data.Exception.EscapeMarkdown()
                });
            }

            body.Sections = sections.ToArray();

            return(body);
        }