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/ diff --git a/Log4Slack/SlackAppender.cs b/Log4Slack/SlackAppender.cs index bfef684..da4f44e 100644 --- a/Log4Slack/SlackAppender.cs +++ b/Log4Slack/SlackAppender.cs @@ -20,6 +20,8 @@ public class SlackAppender : AppenderSkeleton { private readonly Process _currentProcess = Process.GetCurrentProcess(); private List Mappings = new List(); + public static List ExceptionTypesToIgnore = new List(); + /// /// Slack token. /// https://api.slack.com/ @@ -79,7 +81,12 @@ public class SlackAppender : AppenderSkeleton { public Mapping mapping { set { Mappings.Add(value); } } - protected override void Append(log4net.Core.LoggingEvent loggingEvent) { + protected override void Append(log4net.Core.LoggingEvent loggingEvent) + { + + if (ExceptionTypesToIgnore.Contains(loggingEvent.ExceptionObject.GetType())) + return; + // Initialze the Slack client var slackClient = new SlackClient(WebhookUrl.Expand()); var attachments = new List(); @@ -140,6 +147,16 @@ protected override void Append(log4net.Core.LoggingEvent loggingEvent) { } } + + public static class PublicExtensions + { + public static void Add(this List list) + { + list.Add(typeof(T)); + } + } + + internal static class Extensions { public static string Expand(this string text) { return text != null ? Environment.ExpandEnvironmentVariables(text) : null; diff --git a/Log4SlackTesting/Program.cs b/Log4SlackTesting/Program.cs index 3fe5e18..923dc26 100644 --- a/Log4SlackTesting/Program.cs +++ b/Log4SlackTesting/Program.cs @@ -4,11 +4,19 @@ using System.Text; using System.Threading.Tasks; using log4net; +using Log4Slack; namespace Log4SlackTesting { class Program { static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); + + // Add exception to ignore + SlackAppender.ExceptionTypesToIgnore.Add(typeof(UnauthorizedAccessException)); + + // Or add exception to ignore using extension + SlackAppender.ExceptionTypesToIgnore.Add(); + var logger = LogManager.GetLogger(typeof(Program)); logger.Info("I know he can get the job, but can he do the job?"); logger.Debug("I'm not arguing that with you."); @@ -23,7 +31,11 @@ static void Main(string[] args) { logger.Fatal("That's it. It's over.", new EncoderFallbackException("Could not fall backwards.")); + logger.Error("This should be ignored.", new UnauthorizedAccessException("No.")); + logger.Error("This as well.", new InvalidCastException("No.")); + Console.ReadKey(); } } } +