Example #1
0
        //// ----------------------------------------------------------------------------------------------------------

        private void SendToSlack(AsyncLogEventInfo info)
        {
            var message = Layout.Render(info.LogEvent);
            var slack = SlackMessageBuilder
                .Build(this.WebHookUrl)
                .OnError(e => info.Continuation(e))
                .WithMessage(message);

            if (!String.IsNullOrWhiteSpace(this.Channel.Render(info.LogEvent)))
                slack.ToChannel(this.Channel.Render(info.LogEvent));

            if (!String.IsNullOrWhiteSpace(this.Icon))
                slack.WithIcon(this.Icon);

            if (!String.IsNullOrWhiteSpace(this.Username.Render(info.LogEvent)))
                slack.AsUser(this.Username.Render(info.LogEvent));

            if (!this.Compact)
            {
                var attachment = new Attachment(message);
                attachment.Color = this.GetSlackColorFromLogLevel(info.LogEvent.Level);

                var exception = info.LogEvent.Exception;
                if (exception != null)
                {
                    attachment.Fields.Add(new Field("Type") { Value = exception.GetType().FullName, Short = true });
                    attachment.Fields.Add(new Field("Message") { Value = exception.Message, Short = true });

                    if (!String.IsNullOrWhiteSpace(exception.StackTrace))
                        attachment.Fields.Add(new Field("Stack Trace") { Value = "```" + exception.StackTrace + "```" });
                }

                attachment.Fields.Add(new Field("Process Name") { Value = String.Format("{0}\\{1}", (_currentProcess.MachineName != "." ? _currentProcess.MachineName : System.Environment.MachineName), _currentProcess.ProcessName), Short = true });
                attachment.Fields.Add(new Field("Process PID") { Value = _currentProcess.Id.ToString(), Short = true });

                slack.AddAttachment(attachment);
            }

            slack.Send();
        }
        //// ----------------------------------------------------------------------------------------------------------

        public SlackMessageBuilder AddAttachment(Attachment attachment)
        {
            this._payload.Attachments.Add(attachment);

            return this;
        }