From 43d82ef6253cd99ead4982c2d44777ffd0558a12 Mon Sep 17 00:00:00 2001 From: walterlv Date: Tue, 20 Aug 2024 12:21:03 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=A1=BA=E5=BA=8F?= =?UTF-8?q?=E4=B8=8D=E5=BD=B1=E5=93=8D=E8=BF=87=E6=BB=A4=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TagFilterManagerTests.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs b/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs index 954c51d..2adcde3 100644 --- a/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs +++ b/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs @@ -84,6 +84,19 @@ public void 单个任一和包含标签() Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); } + [TestMethod("单个任一和包含标签,只要有一个标签匹配即允许。且标签顺序不影响。")] + public void 单个任一和包含标签且标签顺序不影响() + { + var filter = CreateFilter("+Bar,Foo"); + Assert.IsFalse(filter.IsTagEnabled("[Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[Bar] Message")); + Assert.IsTrue(filter.IsTagEnabled("[Foo][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Bar] Message")); + Assert.IsTrue(filter.IsTagEnabled("[xxxx][Foo][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); + } + [TestMethod("单个任一和排除标签,只要有一个标签匹配即不允许。")] public void 单个任一和排除标签() { @@ -97,6 +110,19 @@ public void 单个任一和排除标签() Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); } + [TestMethod("单个任一和排除标签,只要有一个标签匹配即不允许。且标签顺序不影响。")] + public void 单个任一和排除标签且标签顺序不影响() + { + var filter = CreateFilter("-Bar,Foo"); + Assert.IsTrue(filter.IsTagEnabled("[Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[Foo][Bar] Message")); + Assert.IsTrue(filter.IsTagEnabled("[xxxx][Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Foo][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); + } + [TestMethod("单个包含和排除标签,只要有一个标签匹配即不允许。")] public void 单个包含和排除标签() { @@ -110,6 +136,19 @@ public void 单个包含和排除标签() Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); } + [TestMethod("单个包含和排除标签,只要有一个标签匹配即不允许。且标签顺序不影响。")] + public void 单个包含和排除标签且标签顺序不影响() + { + var filter = CreateFilter("-Bar,+Foo"); + Assert.IsTrue(filter.IsTagEnabled("[Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[Foo][Bar] Message")); + Assert.IsTrue(filter.IsTagEnabled("[xxxx][Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Foo][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); + } + [TestMethod("单个任一和包含和排除标签,只要有一个排除标签匹配即不允许,否则只要有一个标签匹配即允许。")] public void 单个任一和包含和排除标签() { From bceacc6fad9ffc460698721119edcd62dd5b64c0 Mon Sep 17 00:00:00 2001 From: walterlv Date: Tue, 20 Aug 2024 12:30:37 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=85=88=E5=8C=85?= =?UTF-8?q?=E5=90=AB=E5=86=8D=E4=BB=BB=E4=B8=80=E7=9A=84=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Writers/Helpers/TagFilterManager.cs | 19 ++++++++----------- .../TagFilterManagerTests.cs | 13 +++++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs b/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs index 7708251..3b3f2ba 100644 --- a/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs +++ b/src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs @@ -75,19 +75,16 @@ internal bool IsTagEnabled(string text) } // 如果有任一标签,则匹配一个即可。 任一满足 = 任一满足 || AnyFilterTags.Contains(tag); - if (任一满足) + // 如果有包含标签,则匹配一个,直到全部匹配。 + if (!包含满足 && IncludingFilterTags.Count > 0) { - // 如果有包含标签,则匹配一个,直到全部匹配。 - if (!包含满足 && IncludingFilterTags.Count > 0) + if (includingTags.Contains(tag)) { - if (includingTags.Contains(tag)) - { - includingTags.Remove(tag); - } - if (includingTags.Count is 0) - { - 包含满足 = true; - } + includingTags.Remove(tag); + } + if (includingTags.Count is 0) + { + 包含满足 = true; } } } diff --git a/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs b/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs index 2adcde3..982ce1c 100644 --- a/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs +++ b/tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs @@ -97,6 +97,19 @@ public void 单个任一和包含标签且标签顺序不影响() Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); } + [TestMethod("单个任一和包含标签,只要有一个标签匹配即允许。且标签顺序不影响2。")] + public void 单个任一和包含标签且标签顺序不影响2() + { + var filter = CreateFilter("+Foo,Bar"); + Assert.IsFalse(filter.IsTagEnabled("[Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[Bar] Message")); + Assert.IsTrue(filter.IsTagEnabled("[Foo][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Foo] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx][Bar] Message")); + Assert.IsTrue(filter.IsTagEnabled("[xxxx][Foo][Bar] Message")); + Assert.IsFalse(filter.IsTagEnabled("[xxxx] Message")); + } + [TestMethod("单个任一和排除标签,只要有一个标签匹配即不允许。")] public void 单个任一和排除标签() {