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); }
//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); }