From 921c0d07130e123ea71ac965cc5a51152adb8624 Mon Sep 17 00:00:00 2001 From: Markus Johansson Date: Wed, 16 Oct 2019 18:02:37 +0200 Subject: [PATCH 1/3] Ingore VS-files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 2f72fc4..4b4d266 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,4 @@ $RECYCLE.BIN/ # Mac desktop service store files .DS_Store +.vs/ From 79eb7bffcb6e912402c6aaf3cce72d9d164361b2 Mon Sep 17 00:00:00 2001 From: Markus Johansson Date: Wed, 16 Oct 2019 18:36:58 +0200 Subject: [PATCH 2/3] Added support for always showing inner exception --- Log4Slack/SlackAppender.cs | 41 +++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/Log4Slack/SlackAppender.cs b/Log4Slack/SlackAppender.cs index bfef684..c7f490d 100644 --- a/Log4Slack/SlackAppender.cs +++ b/Log4Slack/SlackAppender.cs @@ -120,17 +120,38 @@ protected override void Append(log4net.Core.LoggingEvent loggingEvent) { 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 + if (AddExceptionTraceField && !string.IsNullOrWhiteSpace(exception.StackTrace)) + { + var parts = SlackMessageHelper.SplitIntoChunksOf2kChars(exception.StackTrace); + 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(name, Value: SlackMessageHelper.FormatAsPre(parts[idx]))); } } theAttachment.Fields.Insert(0, new Field("Exception Message", Value: exception.Message)); } + if (exception != null && exception.InnerException != null) + { + theAttachment.Fields.Add(new Field("---- Inner Exception ----", Value: string.Empty, Short: false)); + theAttachment.Fields.Add(new Field("Inner Exception Type", Value: exception.InnerException.GetType().Name, Short: true)); + theAttachment.Fields.Add(new Field("Inner Exception Message", Value: exception.InnerException.Message)); + + if (AddExceptionTraceField && !string.IsNullOrWhiteSpace(exception.InnerException.StackTrace)) + { + + var parts = SlackMessageHelper.SplitIntoChunksOf2kChars(exception.InnerException.StackTrace); + for (int idx = parts.Length - 1; idx >= 0; idx--) + { + var name = "Inner exception Trace" + (idx > 0 ? string.Format(" {0}", idx + 1) : null); + theAttachment.Fields.Add(new Field(name, Value: SlackMessageHelper.FormatAsPre(parts[idx]))); + } + } + + } + attachments.Add(theAttachment); } @@ -140,6 +161,20 @@ protected override void Append(log4net.Core.LoggingEvent loggingEvent) { } } + internal static class SlackMessageHelper + { + public static string FormatAsPre(string content) + { + return $"```{content.Replace("```", "'''")}```"; + } + + public static string[] SplitIntoChunksOf2kChars(string content) + { + return content.SplitOn(1990).ToArray(); + } + + } + internal static class Extensions { public static string Expand(this string text) { return text != null ? Environment.ExpandEnvironmentVariables(text) : null; From 3d18435179c448c463eadd95a5f967430f96316b Mon Sep 17 00:00:00 2001 From: Markus Johansson Date: Wed, 16 Oct 2019 18:40:40 +0200 Subject: [PATCH 3/3] Added setting for inner exception --- Log4Slack/SlackAppender.cs | 7 ++++++- Log4SlackTesting/App.config | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Log4Slack/SlackAppender.cs b/Log4Slack/SlackAppender.cs index c7f490d..15508eb 100644 --- a/Log4Slack/SlackAppender.cs +++ b/Log4Slack/SlackAppender.cs @@ -62,6 +62,11 @@ public class SlackAppender : AppenderSkeleton { /// public bool AddExceptionTraceField { get; set; } + /// + /// Indicates whether or not to include the inner exception in the message. + /// + public bool AddInnerException { get; set; } + /// /// Indicates whether or not to append the logger name to the Stack username. /// @@ -133,7 +138,7 @@ protected override void Append(log4net.Core.LoggingEvent loggingEvent) { theAttachment.Fields.Insert(0, new Field("Exception Message", Value: exception.Message)); } - if (exception != null && exception.InnerException != null) + if (AddInnerException && exception != null && exception.InnerException != null) { theAttachment.Fields.Add(new Field("---- Inner Exception ----", Value: string.Empty, Short: false)); theAttachment.Fields.Add(new Field("Inner Exception Type", Value: exception.InnerException.GetType().Name, Short: true)); diff --git a/Log4SlackTesting/App.config b/Log4SlackTesting/App.config index c1f099e..18f899d 100644 --- a/Log4SlackTesting/App.config +++ b/Log4SlackTesting/App.config @@ -34,6 +34,7 @@ +