Skip to content

Commit

Permalink
Merge pull request #20 from dotnet-campus/t/lvyi/order
Browse files Browse the repository at this point in the history
解决顺序对标签过滤产生的影响
  • Loading branch information
lindexi authored Aug 20, 2024
2 parents 6d0bf04 + bceacc6 commit 4212e79
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/dotnetCampus.Logger/Writers/Helpers/TagFilterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
52 changes: 52 additions & 0 deletions tests/dotnetCampus.Logger.Tests/TagFilterManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,32 @@ 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("单个任一和包含标签,只要有一个标签匹配即允许。且标签顺序不影响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 单个任和排除标签()
{
Expand All @@ -97,6 +123,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 单个包含和排除标签()
{
Expand All @@ -110,6 +149,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 单个任和包含和排除标签()
{
Expand Down

0 comments on commit 4212e79

Please sign in to comment.