Пример #1
0
        public void Slack(string title, object message, SlackType type = SlackType.Info)
        {
            List <SlackDto.Attachment> attachmentList = new List <SlackDto.Attachment>();

            SlackDto.Attachment attachment = new SlackDto.Attachment();
            attachment.fields = new List <SlackDto.Field>();
            attachment.fields.Add(new SlackDto.Field {
                title = _setting.Title, value = ""
            });


            if (message.GetType().IsGenericType&& message.GetType().GetGenericTypeDefinition().IsAssignableFrom(typeof(List <>)))
            {
                IList collection = (IList)message;

                for (int i = 0; i < collection.Count; i++)
                {
                    FillAttachment(collection[i], attachment);
                }
            }
            else
            {
                FillAttachment(message, attachment);
            }



            attachment.title      = title;
            attachment.title_link = _setting.TitleUrl;
            attachmentList.Add(attachment);
            string serializedContent = JsonConvert.SerializeObject(attachmentList.ToArray());

            _slackClient.SendMessage(serializedContent, type, true);
        }
Пример #2
0
        private void FillAttachment(object item, SlackDto.Attachment attachment)
        {
            foreach (PropertyInfo pi in item.GetType().GetProperties())
            {
                string value = pi.GetValue(item, null)?.ToString();

                IEnumerable <object> enumerable = pi.GetValue(item, null) as IEnumerable <object>;

                if (enumerable != null)
                {
                    value = string.Join(", ", enumerable);
                }


                attachment.fields.Add(new SlackDto.Field {
                    title = pi.Name, value = value
                });
            }
        }
Пример #3
0
        public void Slack(Exception exp, string pretext = "")
        {
            List <SlackDto.Attachment> attachmentList = new List <SlackDto.Attachment>();

            SlackDto.Attachment attachment = new SlackDto.Attachment();
            attachment.fields = new List <SlackDto.Field>();
            attachment.fields.Add(new SlackDto.Field {
                title = "Message", value = exp.Message != null ? exp.Message.ToString().Trim() : ""
            });
            attachment.fields.Add(new SlackDto.Field {
                title = "Method Name", value = new StackTrace(new StackFrame(2)).GetFrame(0).ToString().Trim()
            });
            attachment.fields.Add(new SlackDto.Field {
                title = "InnerException", value = exp.InnerException != null ? exp.InnerException.ToString().Trim() : ""
            });
            attachment.fields.Add(new SlackDto.Field {
                title = "Source", value = exp.Source != null ? exp.Source.ToString().Trim() : ""
            });
            attachment.fields.Add(new SlackDto.Field {
                title = "Data", value = (exp.Data != null ? exp.Data.ToString() : "").Trim()
            });
            attachment.fields.Add(new SlackDto.Field {
                title = "StackTrace", value = exp.StackTrace != null ? exp.StackTrace.ToString().Trim() : ""
            });
            attachment.fields.Add(new SlackDto.Field {
                title = "HResult", value = exp.HResult.ToString().Trim()
            });
            attachment.fields.Add(new SlackDto.Field {
                title = "TargetSite", value = exp.TargetSite != null ? exp.HResult.ToString().Trim() : ""
            });

            attachment.pretext    = pretext;
            attachment.title      = _setting.Title;
            attachment.title_link = _setting.TitleUrl;

            attachmentList.Add(attachment);
            string serializedContent = JsonConvert.SerializeObject(attachmentList.ToArray());

            _slackClient.SendMessage(serializedContent, SlackType.Error, true);
        }