コード例 #1
0
ファイル: SlackAppender.cs プロジェクト: perosb/Log4Slack
        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(); // 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, username, Channel.Expand(), IconUrl.Expand(), IconEmoji.Expand(), attachments);
        }
コード例 #2
0
        public override void ActivateOptions()
        {
            base.ActivateOptions();

            if (string.IsNullOrEmpty(WebhookUrl))
            {
                throw new ArgumentException("WebhookUrl not set!");
            }

            this.TeamsClient = new MicrosoftTeamsClient(WebhookUrl.Expand());
        }
コード例 #3
0
        private string SplitWebUrl(int index)
        {
            if (!WebhookUrl.StartsWith("http", StringComparison.CurrentCulture))
            {
                WebhookUrl = "https://" + WebhookUrl;
            }
            var split = WebhookUrl.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            return(split.Length < index
                ? string.Empty
                : split[index]);
        }
コード例 #4
0
        protected override void Append(LoggingEvent loggingEvent)
        {
            // Initialze the Flock client
            var flockClient = new FlockClient(WebhookUrl.Expand());
            var attachments = new List <Attachment>();

            if (AddAttachment)
            {
                attachments.Add(BuildAttachment(loggingEvent));
            }

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

            flockClient.PostMessageAsync(formattedMessage, Proxy, attachments);
        }
コード例 #5
0
        /// <inheritdoc />
        public override int GetHashCode()
        {
            unchecked
            {
                int hashCode = ErrorMessage != null?ErrorMessage.GetHashCode() : 0;

                hashCode = (hashCode * 397) ^ SendDelay.GetHashCode();
                hashCode = (hashCode * 397) ^ (WebhookUrl != null ? WebhookUrl.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ InstanceStatuses.GetHashCode();
                hashCode = (hashCode * 397) ^ WebhookStatuses.GetHashCode();
                hashCode = (hashCode * 397) ^ StatusNotificationsOn.GetHashCode();
                hashCode = (hashCode * 397) ^ AckNotificationsOn.GetHashCode();
                hashCode = (hashCode * 397) ^ ChatUpdateOn.GetHashCode();
                hashCode = (hashCode * 397) ^ VideoUploadOn.GetHashCode();
                hashCode = (hashCode * 397) ^ (Proxy != null ? Proxy.GetHashCode() : 0);
                hashCode = (hashCode * 397) ^ GuaranteedHooks.GetHashCode();
                hashCode = (hashCode * 397) ^ IgnoreOldMessages.GetHashCode();
                hashCode = (hashCode * 397) ^ OldMessagesPeriod.GetHashCode();
                hashCode = (hashCode * 397) ^ ProcessArchive.GetHashCode();
                hashCode = (hashCode * 397) ^ DisableDialogsArchive.GetHashCode();
                hashCode = (hashCode * 397) ^ ParallelHooks.GetHashCode();
                return(hashCode);
            }
        }
コード例 #6
0
        //public SlackClient slackClient;
        //public SlackHttpClient httpClient;

        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            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?.FirstOrDefault(m => m.level.Equals(loggingEvent.Level.DisplayName, StringComparison.InvariantCultureIgnoreCase));
                if (mapping != null)
                {
                    var color = Color.FromName(mapping.backColor);
                    var hex   = color.IsKnownColor ? $"#{color.R:X2}{color.G:X2}{color.B:X2}" : mapping.backColor;
                    theAttachment.Color = !string.IsNullOrEmpty(hex) ? hex : theAttachment.Color;
                }

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

                // Add exception fields if exception occurred
                var exception = loggingEvent.ExceptionObject;
                if (exception != null)
                {
                    theAttachment.Fields.Insert(0, new Field("Exception Type", exception.GetType().Name, 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 ? $" {idx + 1}" : null);
                            theAttachment.Fields.Insert(0, new Field(name, "```" + parts[idx].Replace("```", "'''") + "```"));
                        }
                    }

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

                attachments.Add(theAttachment);
            }

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

            SlackHttpClient.PostMessageAsync(formattedMessage,
                                             WebhookUrl.Expand(),
                                             username,
                                             Channel.Expand(),
                                             IconUrl.Expand(),
                                             IconEmoji.Expand(),
                                             attachments,
                                             LinkNames);
        }
コード例 #7
0
ファイル: SlackAppender.cs プロジェクト: arb-rasmus/Log4Slack
        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);
        }
コード例 #8
0
        protected override void Append(log4net.Core.LoggingEvent loggingEvent)
        {
            var message = new Message
            {
                Text = string.Empty,
                Card = new Card()
                {
                    Sections = new List <Section>
                    {
                        new Section
                        {
                            Widgets = new List <Widget>()
                        }
                    }
                }
            };

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

            text.Append(formattedMessage);
            var exception = loggingEvent.ExceptionObject;

            if (exception != null)
            {
                if (!string.IsNullOrEmpty(exception.StackTrace))
                {
                    text.AppendLine(exception.StackTrace);
                }

                message.Card.Sections[0].Widgets.Add(new Widget
                {
                    KeyValue = new KeyValue
                    {
                        TopLabel = "Exception Type",
                        Content  = exception.GetType().Name,
                    }
                }
                                                     );
            }
            message.Text = text.ToString();

            message.Card.Sections[0].Widgets.AddRange(new List <Widget>
            {
                new Widget
                {
                    KeyValue = new KeyValue
                    {
                        TopLabel = "Process",
                        Content  = currentProcess.ProcessName,
                    },
                },
                new Widget
                {
                    KeyValue = new KeyValue
                    {
                        TopLabel = "Machine",
                        Content  = Environment.MachineName,
                    },
                },
                new Widget
                {
                    KeyValue = new KeyValue
                    {
                        TopLabel = "Environment",
                        Content  = ProgramEnvironment,
                    },
                },
            });

            HangoutsClient.PostMessageAsync(WebhookUrl.Expand(), message);
        }