Ejemplo n.º 1
0
        private void SendToSlack(AsyncLogEventInfo info)
        {
            var message = RenderLogEvent(Layout, info.LogEvent);

            var webHookUrl = GetWebHookUrl(info.LogEvent);
            var slack      = SlackMessageBuilder
                             .Build(webHookUrl)
                             .OnError(e => info.Continuation(e))
                             .WithMessage(message);

            if (this.ShouldIncludeProperties(info.LogEvent) || this.ContextProperties.Count > 0)
            {
                var        color      = this.GetSlackColorFromLogLevel(info.LogEvent.Level);
                Attachment attachment = new Attachment(info.LogEvent.Message)
                {
                    Color = color
                };
                var allProperties = this.GetAllProperties(info.LogEvent);
                foreach (var property in allProperties)
                {
                    if (string.IsNullOrEmpty(property.Key))
                    {
                        continue;
                    }

                    var propertyValue = property.Value?.ToString();
                    if (string.IsNullOrEmpty(propertyValue))
                    {
                        continue;
                    }

                    attachment.Fields.Add(new Field(property.Key)
                    {
                        Value = propertyValue, Short = true
                    });
                }
                if (attachment.Fields.Count > 0)
                {
                    slack.AddAttachment(attachment);
                }
            }

            var exception = info.LogEvent.Exception;

            if (!this.Compact && exception != null)
            {
                var color = this.GetSlackColorFromLogLevel(info.LogEvent.Level);
                var exceptionAttachment = new Attachment(null)
                {
                    Color = color,
                    Text  = $"*Exception*\n```{exception}```"
                };
                slack.AddAttachment(exceptionAttachment);
            }

            slack.Send();
        }
Ejemplo n.º 2
0
        private void SendToSlack(AsyncLogEventInfo info)
        {
            var message = RenderLogEvent(Layout, info.LogEvent);

            var slack = SlackMessageBuilder
                        .Build(this.WebHookUrl)
                        .OnError(e => info.Continuation(e))
                        .WithMessage(message);

            if (this.ShouldIncludeProperties(info.LogEvent) || this.ContextProperties.Count > 0)
            {
                var        color      = this.GetSlackColorFromLogLevel(info.LogEvent.Level);
                Attachment attachment = new Attachment(info.LogEvent.Message)
                {
                    Color = color
                };
                var allProperties = this.GetAllProperties(info.LogEvent);
                foreach (var property in allProperties)
                {
                    if (string.IsNullOrEmpty(property.Key))
                    {
                        continue;
                    }

                    var propertyValue = property.Value?.ToString();
                    if (string.IsNullOrEmpty(propertyValue))
                    {
                        continue;
                    }

                    attachment.Fields.Add(new Field(property.Key)
                    {
                        Value = propertyValue, Short = true
                    });
                }
                if (attachment.Fields.Count > 0)
                {
                    slack.AddAttachment(attachment);
                }
            }

            var exception = info.LogEvent.Exception;

            if (!this.Compact && exception != null)
            {
                var stackTrace = RenderLogEvent(Layouts.Layout.FromString("${exception:format=tostring}"), info.LogEvent);

                var color = this.GetSlackColorFromLogLevel(info.LogEvent.Level);
                var exceptionAttachment = new Attachment(exception.Message)
                {
                    Color = color
                };
                exceptionAttachment.Fields.Add(new Field("StackTrace")
                {
                    Title = $"Type: {exception.GetType().ToString()}",
                    Value = stackTrace ?? "N/A"
                });

                slack.AddAttachment(exceptionAttachment);
            }

            slack.Send();
        }
Ejemplo n.º 3
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))
            {
                slack.ToChannel(this.Channel);
            }

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

            if (!String.IsNullOrWhiteSpace(this.Username))
            {
                slack.AsUser(this.Username);
            }

            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.ProcessName), Short = true
                });
                attachment.Fields.Add(new Field("Process PID")
                {
                    Value = _currentProcess.Id.ToString(), Short = true
                });

                slack.AddAttachment(attachment);
            }

            slack.Send();
        }
Ejemplo n.º 4
0
        private void SendToSlack(AsyncLogEventInfo info)
        {
            var message = RenderLogEvent(Layout, info.LogEvent);

            var slack = SlackMessageBuilder
                        .Build(this.WebHookUrl)
                        .OnError(e => info.Continuation(e))
                        .WithMessage(message);

            var        color      = this.GetSlackColorFromLogLevel(info.LogEvent.Level);
            Attachment attachment = new Attachment(info.LogEvent.Message)
            {
                Color = color
            };

            if (this.ShouldIncludeProperties(info.LogEvent) || this.ContextProperties.Count > 0)
            {
                var allProperties = this.GetAllProperties(info.LogEvent);
                foreach (var property in allProperties)
                {
                    if (string.IsNullOrEmpty(property.Key))
                    {
                        continue;
                    }

                    var propertyValue = property.Value?.ToString();
                    if (string.IsNullOrEmpty(propertyValue))
                    {
                        continue;
                    }

                    attachment.Fields.Add(new Field(property.Key)
                    {
                        Value = propertyValue, Short = true
                    });
                }
            }

            var exception = info.LogEvent.Exception;

            if (!this.Compact && exception != null)
            {
                attachment.Fields.Add(new Field("Exception Message")
                {
                    Value = exception.Message, Short = false
                });
                attachment.Fields.Add(new Field("Exception Type")
                {
                    Value = exception.GetType().Name, Short = true
                });

                if (!string.IsNullOrWhiteSpace(exception.StackTrace))
                {
                    var parts = exception.StackTrace.SplitOn(stackTraceChunk).ToArray(); // Split call stack into consecutive fields of ~2k characters
                    for (int idx = 0; idx < parts.Length; idx++)
                    {
                        var name = "StackTrace" + (idx > 0 ? $" {idx + 1}" : null);
                        attachment.Fields.Add(new Field(name)
                        {
                            Value = "```" + parts[idx].Replace("```", "'''") + "```"
                        });
                    }
                }
            }

            if (attachment.Fields.Count > 0)
            {
                slack.AddAttachment(attachment);
            }

            slack.Send();
        }
Ejemplo n.º 5
0
        private void SendToSlack(AsyncLogEventInfo info)
        {
            var message = RenderLogEvent(Layout, info.LogEvent);

            SlackMessageBuilder slack;

            if (UseProxy)
            {
                var proxy = new ProxySettings
                {
                    Host = ProxyHost, Port = ProxyPort, User = ProxyUser, Password = ProxyPassword
                };

                slack = SlackMessageBuilder.Build(WebHookUrl, proxy)
                        .OnError(e => info.Continuation(e))
                        .WithMessage(message);
            }
            else
            {
                slack = SlackMessageBuilder
                        .Build(WebHookUrl)
                        .OnError(e => info.Continuation(e))
                        .WithMessage(message);
            }


            if (ShouldIncludeProperties(info.LogEvent) || ContextProperties.Count > 0)
            {
                var        color      = GetSlackColorFromLogLevel(info.LogEvent.Level);
                Attachment attachment = new Attachment(info.LogEvent.Message)
                {
                    Color = color
                };
                var allProperties = GetAllProperties(info.LogEvent);
                foreach (var property in allProperties)
                {
                    if (string.IsNullOrEmpty(property.Key))
                    {
                        continue;
                    }

                    var propertyValue = property.Value?.ToString();
                    if (string.IsNullOrEmpty(propertyValue))
                    {
                        continue;
                    }

                    attachment.Fields.Add(new Field(property.Key)
                    {
                        Value = propertyValue, Short = true
                    });
                }

                if (attachment.Fields.Count > 0)
                {
                    slack.AddAttachment(attachment);
                }
            }

            var exception = info.LogEvent.Exception;

            if (!Compact && exception != null)
            {
                var color = GetSlackColorFromLogLevel(info.LogEvent.Level);
                var exceptionAttachment = new Attachment(exception.Message)
                {
                    Color = color
                };
                exceptionAttachment.Fields.Add(new Field("StackTrace")
                {
                    Title = $"Type: {exception.GetType()}",
                    Value = exception.StackTrace ?? "N/A"
                });

                slack.AddAttachment(exceptionAttachment);
            }

            slack.Send();
        }