Skip to content
This repository was archived by the owner on Dec 13, 2018. It is now read-only.

Commit 8bc28d3

Browse files
authored
Add support for category wildcards (#924)
1 parent 8647240 commit 8bc28d3

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

src/Microsoft.Extensions.Logging/LoggerRuleSelector.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Microsoft.Extensions.Logging
77
{
88
internal class LoggerRuleSelector
99
{
10+
private static readonly char[] WildcardChar = { '*' };
11+
1012
public void Select(LoggerFilterOptions options, Type providerType, string category, out LogLevel? minLevel, out Func<string, string, LogLevel, bool> filter)
1113
{
1214
filter = null;
@@ -47,9 +49,22 @@ private static bool IsBetter(LoggerFilterRule rule, LoggerFilterRule current, st
4749
return false;
4850
}
4951

50-
if (rule.CategoryName != null && !category.StartsWith(rule.CategoryName, StringComparison.OrdinalIgnoreCase))
52+
if (rule.CategoryName != null)
5153
{
52-
return false;
54+
var categoryParts = rule.CategoryName.Split(WildcardChar);
55+
if (categoryParts.Length > 2)
56+
{
57+
throw new InvalidOperationException("Only one wildcard character is allowed in category name.");
58+
}
59+
60+
var prefix = categoryParts[0];
61+
var suffix = categoryParts.Length > 1 ? categoryParts[1] : string.Empty;
62+
63+
if (!category.StartsWith(prefix, StringComparison.OrdinalIgnoreCase) ||
64+
!category.EndsWith(suffix, StringComparison.OrdinalIgnoreCase))
65+
{
66+
return false;
67+
}
5368
}
5469

5570
if (current?.ProviderName != null)

test/Microsoft.Extensions.Logging.Test/LoggerFilterTest.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using Microsoft.Extensions.Configuration;
@@ -429,6 +430,23 @@ public void DefaultCategoryIsCaseInsensitive()
429430
Assert.Null(options.Value.Rules.Single().CategoryName);
430431
}
431432

433+
[Fact]
434+
public void MultipleWildcardsAreNotAllowed()
435+
{
436+
var options = new LoggerFilterOptions()
437+
{
438+
Rules = { new LoggerFilterRule(providerName: null, categoryName: "*A*", logLevel: null, filter: null)}
439+
};
440+
var testSink1 = new TestSink();
441+
var loggerFactory = new LoggerFactory(new[]
442+
{
443+
new TestLoggerProvider2(testSink1)
444+
}, options);
445+
446+
var exception = Assert.Throws<InvalidOperationException>(() => loggerFactory.CreateLogger("Category"));
447+
Assert.Equal("Only one wildcard character is allowed in category name.", exception.Message);
448+
}
449+
432450
[Theory]
433451
[MemberData(nameof(FilterTestData))]
434452
public void FilterTest(LoggerFilterOptions options, (string category, LogLevel level, bool expectInProvider1, bool expectInProvider2) message)
@@ -450,7 +468,6 @@ public void FilterTest(LoggerFilterOptions options, (string category, LogLevel l
450468
Assert.Equal(message.expectInProvider2 ? 1 : 0, testSink2.Writes.Count);
451469
}
452470

453-
454471
public static TheoryData<LoggerFilterOptions, (string, LogLevel, bool, bool)> FilterTestData =
455472
new TheoryData<LoggerFilterOptions, (string, LogLevel, bool, bool)>()
456473
{
@@ -580,6 +597,40 @@ public void FilterTest(LoggerFilterOptions options, (string category, LogLevel l
580597
},
581598
("Category.Sub", LogLevel.Trace, true, false)
582599
},
600+
{ // Wildcards allowed in category names
601+
new LoggerFilterOptions()
602+
{
603+
MinLevel = LogLevel.Critical,
604+
Rules =
605+
{
606+
new LoggerFilterRule(typeof(TestLoggerProvider).FullName, "Category.*.Sub", LogLevel.Trace, null),
607+
new LoggerFilterRule(null, null, LogLevel.Critical, null)
608+
}
609+
},
610+
("Category.B.Sub", LogLevel.Trace, true, false)
611+
},
612+
{ // Wildcards allowed in the beginning of category names
613+
new LoggerFilterOptions()
614+
{
615+
MinLevel = LogLevel.Critical,
616+
Rules =
617+
{
618+
new LoggerFilterRule(typeof(TestLoggerProvider).FullName, "*.Sub", LogLevel.Trace, null),
619+
}
620+
},
621+
("Category.B.Sub", LogLevel.Trace, true, false)
622+
},
623+
{ // Wildcards allowed in the end of category names
624+
new LoggerFilterOptions()
625+
{
626+
MinLevel = LogLevel.Critical,
627+
Rules =
628+
{
629+
new LoggerFilterRule(typeof(TestLoggerProvider).FullName, "Cat*", LogLevel.Trace, null),
630+
}
631+
},
632+
("Category.B.Sub", LogLevel.Trace, true, false)
633+
}
583634
};
584635
}
585636
}

0 commit comments

Comments
 (0)