Ejemplo n.º 1
0
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            // Initialze the Slack client
            var slackClient = new SlackClient(WebhookUrl);
            var attachments = new List<Attachment>();

            if (AddAttachment) {
                // Set fallback string
                var theAttachment = new Attachment(string.Format("[{0}] {1} in {2} on {3}", loggingEvent.Level.DisplayName, loggingEvent.LoggerName, _currentProcess.ProcessName, Environment.MachineName));

                // Determine attachment color
                switch (loggingEvent.Level.DisplayName.ToLowerInvariant()) {
                    case "warn":
                        theAttachment.Color = "warning";
                        break;
                    case "error":
                    case "fatal":
                        theAttachment.Color = "danger";
                        break;
                }

                // Add attachment fields
                theAttachment.Fields = new List<Field> {
                    new Field("Process") {Value = _currentProcess.ProcessName, Short = true},
                    new Field("Machine") {Value = Environment.MachineName, Short = true}
                };
                if (!UsernameAppendLoggerName)
                    theAttachment.Fields.Insert(0, new Field("Logger") {Value = loggingEvent.LoggerName, Short = true});

                // Add exception fields if exception occurred
                var exception = loggingEvent.ExceptionObject;
                if (exception != null) {
                    if (AddExceptionTraceField && !string.IsNullOrWhiteSpace(exception.StackTrace))
                        theAttachment.Fields.Insert(0, new Field("Exception Trace") { Value = "```" + exception.StackTrace + "```" });
                    theAttachment.Fields.Insert(0, new Field("Exception Type") { Value = exception.GetType().Name, Short = true });
                    theAttachment.Fields.Insert(0, new Field("Exception Message") { Value = exception.Message, Short = true });
                }

                attachments.Add(theAttachment);
            }

            String formattedMessage = loggingEvent.RenderedMessage;
            if (Layout != null)
            {
                using(StringWriter writer = new StringWriter())
                {
                    Layout.Format(writer, loggingEvent);
                    formattedMessage = writer.ToString();
                }
            }

            var message = string.Format("[{0}] {1}", loggingEvent.Level.DisplayName.ToLowerInvariant(), formattedMessage);
            var username = Username;
            if (UsernameAppendLoggerName)
                username += " - " + loggingEvent.LoggerName;

            slackClient.PostMessageAsync(message, username, Channel, IconUrl, IconEmoji, attachments);
        }
Ejemplo n.º 2
0
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            // Initialze the Slack client
            var slackClient = new SlackClient(WebhookUrl.Expand());
            var attachments = new List <Attachment>();

            if (AddAttachment)
            {
                // Set fallback string
                var theAttachment = new Attachment();

                // Determine attachment color
                switch (loggingEvent.Level.DisplayName.ToLowerInvariant())
                {
                case "warn":
                    theAttachment.Color = "warning";
                    break;

                case "error":
                case "fatal":
                    theAttachment.Color = "danger";
                    break;
                }

                //override colors from config if available
                var mapping = Mappings != null
                    ? Mappings.FirstOrDefault(m => m.level.Equals(loggingEvent.Level.DisplayName,
                                                                  StringComparison.InvariantCultureIgnoreCase))
                    : null;

                if (mapping != null)
                {
                    var color = Color.FromName(mapping.backColor);
                    var hex   = color.IsKnownColor
                        ? String.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B)
                        : mapping.backColor;
                    theAttachment.Color = !string.IsNullOrEmpty(hex) ? hex : theAttachment.Color;
                }
                else
                {
                    theAttachment.Color = "#D00000";
                }

                theAttachment.Views =
                    new View(new Html
                {
                    Inline = $"<div style=\"color:red;\"><strong>{_currentProcess.ProcessName} - {Environment.MachineName}</strong></div>",
                    Width  = 0,
                    Height = 0
                });

                //// Add attachment fields
                //theAttachment.Fields = new List<Field> {
                //    new Field("Process", Value: _currentProcess.ProcessName, Short: true),
                //    new Field("Machine", Value: Environment.MachineName, Short: true)
                //};
                //if (!UsernameAppendLoggerName)
                //    theAttachment.Fields.Insert(0, new Field("Logger", Value: loggingEvent.LoggerName, Short: true));

                //// Add exception fields if exception occurred
                //var exception = loggingEvent.ExceptionObject;
                //if (exception != null) {
                //    theAttachment.Fields.Insert(0, new Field("Exception Type", Value: exception.GetType().Name, Short: true));
                //    if (AddExceptionTraceField && !string.IsNullOrWhiteSpace(exception.StackTrace)) {
                //        var parts = exception.StackTrace.SplitOn(1990).ToArray(); // Split call stack into consecutive fields of ~2k characters
                //        for (int idx = parts.Length - 1; idx >= 0; idx--) {
                //            var name = "Exception Trace" + (idx > 0 ? string.Format(" {0}", idx + 1) : null);
                //            theAttachment.Fields.Insert(0, new Field(name, Value: "```" + parts[idx].Replace("```", "'''") + "```"));
                //        }
                //    }

                //    theAttachment.Fields.Insert(0, new Field("Exception Message", Value: exception.Message));
                //}

                attachments.Add(theAttachment);
            }

            var formattedMessage = (Layout != null ? Layout.FormatString(loggingEvent) : loggingEvent.RenderedMessage);

            slackClient.PostMessageAsync(formattedMessage, Proxy, attachments);
        }
Ejemplo n.º 3
0
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            // Initialze the Slack client
            var slackClient = new SlackClient(WebhookUrl.Expand());
            var attachments = new List<Attachment>();

            if (AddAttachment) {
                // Set fallback string
                var theAttachment = new Attachment(string.Format("[{0}] {1} in {2} on {3}", loggingEvent.Level.DisplayName,
                    loggingEvent.LoggerName, _currentProcess.ProcessName, Environment.MachineName));

                // Determine attachment color
                switch (loggingEvent.Level.DisplayName.ToLowerInvariant()) {
                    case "warn":
                        theAttachment.Color = "warning";
                        break;
                    case "error":
                    case "fatal":
                        theAttachment.Color = "danger";
                        break;
                }

                // Add attachment fields
                theAttachment.Fields = new List<Field> {
                    new Field("Process", Value: _currentProcess.ProcessName, Short: true),
                    new Field("Machine", Value: Environment.MachineName, Short: true)
                };
                if (!UsernameAppendLoggerName)
                    theAttachment.Fields.Insert(0, new Field("Logger", Value: loggingEvent.LoggerName, Short: true));

                // Add exception fields if exception occurred
                var exception = loggingEvent.ExceptionObject;
                if (exception != null) {
                    theAttachment.Fields.Insert(0, new Field("Exception Type", Value: exception.GetType().Name, Short: true));
                    if (AddExceptionTraceField && !string.IsNullOrWhiteSpace(exception.StackTrace)) {
                        var parts = exception.StackTrace.SplitOn(1990).ToArray();
                        for(int idx = parts.Length - 1; idx >= 0; idx--) {
                            var name = "Exception Trace" + (idx > 0 ? string.Format(" {0}", idx + 1) : null);
                            theAttachment.Fields.Insert(0, new Field(name, Value: "```" + parts[idx].Replace("```", "'''") + "```"));
                        }
                    }
                    theAttachment.Fields.Insert(0, new Field("Exception Message", Value: exception.Message));
                }

                attachments.Add(theAttachment);
            }

            var formattedMessage = (Layout != null ? Layout.FormatString(loggingEvent) : loggingEvent.RenderedMessage);
            var username = Username.Expand() + (UsernameAppendLoggerName ? " - " + loggingEvent.LoggerName : null);
            slackClient.PostMessageAsync(formattedMessage, username, Channel.Expand(), IconUrl.Expand(), IconEmoji.Expand(), attachments);
        }
Ejemplo n.º 4
0
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            // Initialze the Slack client
            var slackClient = new SlackClient(WebhookUrl);
            var attachments = new List <Attachment>();

            if (AddAttachment)
            {
                // Set fallback string
                var theAttachment = new Attachment(string.Format("[{0}] {1} in {2} on {3}", loggingEvent.Level.DisplayName, loggingEvent.LoggerName, _currentProcess.ProcessName, Environment.MachineName));

                // Determine attachment color
                switch (loggingEvent.Level.DisplayName.ToLowerInvariant())
                {
                case "warn":
                    theAttachment.Color = "warning";
                    break;

                case "error":
                case "fatal":
                    theAttachment.Color = "danger";
                    break;
                }

                // Add attachment fields
                theAttachment.Fields = new List <Field> {
                    new Field("Process")
                    {
                        Value = _currentProcess.ProcessName, Short = true
                    },
                    new Field("Machine")
                    {
                        Value = Environment.MachineName, Short = true
                    }
                };
                if (!UsernameAppendLoggerName)
                {
                    theAttachment.Fields.Insert(0, new Field("Logger")
                    {
                        Value = loggingEvent.LoggerName, Short = true
                    });
                }

                // Add exception fields if exception occurred
                var exception = loggingEvent.ExceptionObject;
                if (exception != null)
                {
                    if (AddExceptionTraceField && !string.IsNullOrWhiteSpace(exception.StackTrace))
                    {
                        theAttachment.Fields.Insert(0, new Field("Exception Trace")
                        {
                            Value = "```" + exception.StackTrace + "```"
                        });
                    }
                    theAttachment.Fields.Insert(0, new Field("Exception Type")
                    {
                        Value = exception.GetType().Name, Short = true
                    });
                    theAttachment.Fields.Insert(0, new Field("Exception Message")
                    {
                        Value = exception.Message, Short = true
                    });
                }

                attachments.Add(theAttachment);
            }

            String formattedMessage = loggingEvent.RenderedMessage;

            if (Layout != null)
            {
                using (StringWriter writer = new StringWriter())
                {
                    Layout.Format(writer, loggingEvent);
                    formattedMessage = writer.ToString();
                }
            }

            var message  = string.Format("[{0}] {1}", loggingEvent.Level.DisplayName.ToLowerInvariant(), formattedMessage);
            var username = Username;

            if (UsernameAppendLoggerName)
            {
                username += " - " + loggingEvent.LoggerName;
            }

            slackClient.PostMessageAsync(message, username, Channel, IconUrl, IconEmoji, attachments);
        }
Ejemplo n.º 5
0
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            // Initialze the Slack client
            var slackClient = new SlackClient(WebhookUrl.Expand());
            var attachments = new List <Attachment>();

            if (AddAttachment)
            {
                // Set fallback string
                var theAttachment = new Attachment(string.Format("[{0}] {1} in {2} on {3}", loggingEvent.Level.DisplayName, loggingEvent.LoggerName, _currentProcess.ProcessName, Environment.MachineName));

                // Determine attachment color
                switch (loggingEvent.Level.DisplayName.ToLowerInvariant())
                {
                case "warn":
                    theAttachment.Color = "warning";
                    break;

                case "error":
                case "fatal":
                    theAttachment.Color = "danger";
                    break;
                }

                //override colors from config if available
                var mapping = Mappings != null?Mappings.FirstOrDefault(m => m.level.Equals(loggingEvent.Level.DisplayName, StringComparison.InvariantCultureIgnoreCase)) : null;

                if (mapping != null)
                {
                    var color = ColorKnown.FromName(mapping.backColor);
                    var hex   = color.IsKnownColor() ? String.Format("#{0:X2}{1:X2}{2:X2}", color.Red, color.Green, color.Blue) : mapping.backColor;
                    theAttachment.Color = !string.IsNullOrEmpty(hex) ? hex : theAttachment.Color;
                }

                // Add attachment fields
                theAttachment.Fields = new List <Field> {
                    new Field("Process", Value: _currentProcess.ProcessName, Short: true),
                    new Field("Machine", Value: Environment.MachineName, Short: true)
                };
                if (!UsernameAppendLoggerName)
                {
                    theAttachment.Fields.Insert(0, new Field("Logger", Value: loggingEvent.LoggerName, Short: true));
                }

                // Add exception fields if exception occurred
                var exception = loggingEvent.ExceptionObject;
                if (exception != null)
                {
                    theAttachment.Fields.Insert(0, new Field("Exception Type", Value: exception.GetType().Name, Short: true));
                    if (AddExceptionTraceField && !string.IsNullOrWhiteSpace(exception.StackTrace))
                    {
                        var parts = exception.StackTrace.SplitOn(1990).ToArray(); // Split call stack into consecutive fields of ~2k characters
                        for (int idx = parts.Length - 1; idx >= 0; idx--)
                        {
                            var name = "Exception Trace" + (idx > 0 ? string.Format(" {0}", idx + 1) : null);
                            theAttachment.Fields.Insert(0, new Field(name, Value: "```" + parts[idx].Replace("```", "'''") + "```"));
                        }
                    }

                    theAttachment.Fields.Insert(0, new Field("Exception Message", Value: exception.Message));
                }

                attachments.Add(theAttachment);
            }

            var formattedMessage = (Layout != null ? Layout.FormatString(loggingEvent) : loggingEvent.RenderedMessage);
            var username         = Username.Expand() + (UsernameAppendLoggerName ? " - " + loggingEvent.LoggerName : null);

            slackClient.PostMessageAsync(formattedMessage, Proxy, username, Channel.Expand(), IconUrl.Expand(), IconEmoji.Expand(), attachments, LinkNames);
        }