예제 #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);
        }
예제 #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(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);
        }