From b4f9b36bfbea56d57c609e8fd1d715a2747780e9 Mon Sep 17 00:00:00 2001 From: tt1366 Date: Tue, 11 Dec 2018 14:59:52 +0800 Subject: [PATCH] Enhance the filter to support filter the exception type name. --- src/Filter/ExceptionTypeFilter.cs | 54 +++++++++ src/log4net.csproj | 5 +- tests/src/Filter/ExceptionTypeFilterTest.cs | 118 ++++++++++++++++++++ tests/src/log4net.Tests.csproj | 1 + 4 files changed, 176 insertions(+), 2 deletions(-) create mode 100644 src/Filter/ExceptionTypeFilter.cs create mode 100644 tests/src/Filter/ExceptionTypeFilterTest.cs diff --git a/src/Filter/ExceptionTypeFilter.cs b/src/Filter/ExceptionTypeFilter.cs new file mode 100644 index 00000000..bccb85ef --- /dev/null +++ b/src/Filter/ExceptionTypeFilter.cs @@ -0,0 +1,54 @@ +using log4net.Core; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace log4net.Filter +{ + public class ExceptionTypeFilter : FilterSkeleton + { + public ExceptionTypeFilter() + { + this.AcceptOnMatch = true; + } + + /// + /// Set the exception type name to filter + /// + public string ExceptionTypeName { get; set; } + + /// + /// when matching + /// Default value is true. + /// + public bool AcceptOnMatch { get; set; } + + public override FilterDecision Decide(LoggingEvent loggingEvent) + { + var myExceptionType = Type.GetType(this.ExceptionTypeName, false); + + if (this.ExceptionTypeName != null + && myExceptionType != null + && loggingEvent.ExceptionObject != null) + { + + var myIsMatched = myExceptionType.IsInstanceOfType(loggingEvent.ExceptionObject); + + if (this.AcceptOnMatch) + { + if (myIsMatched) + return FilterDecision.Accept; + } + else + { + if (myIsMatched) + return FilterDecision.Deny; + } + } + + return FilterDecision.Neutral; + } + } +} diff --git a/src/log4net.csproj b/src/log4net.csproj index 05a7062c..94d9934c 100644 --- a/src/log4net.csproj +++ b/src/log4net.csproj @@ -1,4 +1,4 @@ - +