1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
+ using System ;
4
5
using System . Collections . Generic ;
5
6
using System . Linq ;
6
7
using Microsoft . Extensions . Configuration ;
@@ -429,6 +430,23 @@ public void DefaultCategoryIsCaseInsensitive()
429
430
Assert . Null ( options . Value . Rules . Single ( ) . CategoryName ) ;
430
431
}
431
432
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
+
432
450
[ Theory ]
433
451
[ MemberData ( nameof ( FilterTestData ) ) ]
434
452
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
450
468
Assert . Equal ( message . expectInProvider2 ? 1 : 0 , testSink2 . Writes . Count ) ;
451
469
}
452
470
453
-
454
471
public static TheoryData < LoggerFilterOptions , ( string , LogLevel , bool , bool ) > FilterTestData =
455
472
new TheoryData < LoggerFilterOptions , ( string , LogLevel , bool , bool ) > ( )
456
473
{
@@ -580,6 +597,40 @@ public void FilterTest(LoggerFilterOptions options, (string category, LogLevel l
580
597
} ,
581
598
( "Category.Sub" , LogLevel . Trace , true , false )
582
599
} ,
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
+ }
583
634
} ;
584
635
}
585
636
}
0 commit comments