You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by GitBox <gi...@apache.org> on 2018/12/12 01:17:50 UTC

[GitHub] pctimhk closed pull request #41: Enhance the filter to support filter the exception type name.

pctimhk closed pull request #41: Enhance the filter to support filter the exception type name.
URL: https://github.com/apache/logging-log4net/pull/41
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

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;
+        }
+
+        /// <summary>
+        /// Set the exception type name to filter
+        /// </summary>
+        public string ExceptionTypeName { get; set; }
+
+        /// <summary>
+        /// <see cref="FilterDecision.Accept"/> when matching <see cref="ExceptionTypeName"/>
+        /// Default value is true.
+        /// </summary>
+        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 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
 <!--
 
  Licensed to the Apache Software Foundation (ASF) under one
@@ -354,6 +354,7 @@
     <Compile Include="Filter\DenyAllFilter.cs">
       <SubType>Code</SubType>
     </Compile>
+    <Compile Include="Filter\ExceptionTypeFilter.cs" />
     <Compile Include="Filter\FilterDecision.cs">
       <SubType>Code</SubType>
     </Compile>
@@ -805,4 +806,4 @@
     <PostBuildEvent>
     </PostBuildEvent>
   </PropertyGroup>
-</Project>
+</Project>
\ No newline at end of file
diff --git a/tests/src/Filter/ExceptionTypeFilterTest.cs b/tests/src/Filter/ExceptionTypeFilterTest.cs
new file mode 100644
index 00000000..403722b2
--- /dev/null
+++ b/tests/src/Filter/ExceptionTypeFilterTest.cs
@@ -0,0 +1,118 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+#if NET_2_0
+using System;
+using System.Collections.Generic;
+using System.Xml;
+using log4net.Appender;
+using log4net.Config;
+using log4net.Core;
+using log4net.Filter;
+using log4net.Repository;
+using NUnit.Framework;
+using log4net.Tests.Appender;
+using System.Globalization;
+
+namespace log4net.Tests.Filter
+{
+    [TestFixture]
+    public class ExceptionTypeFilterTest
+    {
+
+#if !NETSTANDARD1_3
+        private CultureInfo _currentCulture;
+        private CultureInfo _currentUICulture;
+
+        [SetUp]
+        public void SetUp()
+        {
+            // set correct thread culture
+            _currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
+            _currentUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture;
+            System.Threading.Thread.CurrentThread.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.InvariantCulture;
+        }
+
+        [TearDown]
+        public void TearDown()
+        {
+            // restore previous culture
+            System.Threading.Thread.CurrentThread.CurrentCulture = _currentCulture;
+            System.Threading.Thread.CurrentThread.CurrentUICulture = _currentUICulture;
+        }
+#endif
+
+        protected ILoggerRepository loggerRepository { get; set; }
+
+        [SetUp]
+        public void SetUpConfiguration()
+        {
+            XmlDocument log4netConfig = new XmlDocument();
+            #region Load log4netConfig
+            log4netConfig.LoadXml(@"
+            <log4net>
+            <appender name=""StringAppender"" type=""log4net.Tests.Appender.StringAppender, log4net.Tests"">
+                <filter type=""log4net.Filter.ExceptionTypeFilter, log4net"">
+                    <ExceptionTypeName value=""log4net.Tests.Filter.ExceptionTypeFilterTestException, log4net.Tests"" />
+                </filter>
+            </appender>
+            <root>
+                <level value=""ALL"" />
+                <appender-ref ref=""MemoryAppender"" />
+            </root>
+            </log4net>");
+            #endregion
+
+            ILoggerRepository rep = LogManager.CreateRepository(Guid.NewGuid().ToString());
+            XmlConfigurator.Configure(rep, log4netConfig["log4net"]);
+
+            loggerRepository = LogManager.CreateRepository(Guid.NewGuid().ToString());
+        }
+
+        [Test]
+        public void TestFilterCustomException()
+        {
+            IAppender[] appenders = LogManager.GetRepository(loggerRepository.Name).GetAppenders();
+            Assert.IsTrue(appenders.Length == 1);
+
+            IAppender appender = Array.Find(appenders, delegate(IAppender a) {
+                    return a.Name == "MemoryAppender";
+                });
+            Assert.IsNotNull(appender);
+
+            StringAppender stringAppender = appender as StringAppender;
+            Assert.IsNotNull(stringAppender);
+
+            ILog log1 = LogManager.GetLogger(loggerRepository.Name, "TestFormatString");
+            Assert.IsNotNull(log1);
+
+            // ***
+            log1.Info("Testing for ExceptionTypeFilterTestException", new ExceptionTypeFilterTestException());
+            Assert.AreEqual("Testing for ExceptionTypeFilterTestException", stringAppender.GetString(), "Test ExceptionTypeFilterTestException can be filter.");
+
+        }
+    }
+
+    public class ExceptionTypeFilterTestException : Exception
+    { }
+
+}
+#endif
diff --git a/tests/src/log4net.Tests.csproj b/tests/src/log4net.Tests.csproj
index f1768ccd..37ab6724 100644
--- a/tests/src/log4net.Tests.csproj
+++ b/tests/src/log4net.Tests.csproj
@@ -190,6 +190,7 @@
     <Compile Include="Core\StringFormatTest.cs">
       <SubType>Code</SubType>
     </Compile>
+    <Compile Include="Filter\ExceptionTypeFilterTest.cs" />
     <Compile Include="Hierarchy\Hierarchy.cs" />
     <Compile Include="Hierarchy\Logger.cs">
       <SubType>Code</SubType>


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services