1
1
using System ;
2
2
using Microsoft . Extensions . Configuration ;
3
+ using Serilog . Core ;
3
4
using Serilog . Events ;
4
5
using Serilog . Settings . Configuration . Tests . Support ;
5
6
using TestDummies ;
@@ -11,7 +12,7 @@ namespace Serilog.Settings.Configuration.Tests
11
12
{
12
13
public class ConfigurationSettingsTests
13
14
{
14
- private static LoggerConfiguration ConfigFromJson ( string jsonString )
15
+ static LoggerConfiguration ConfigFromJson ( string jsonString )
15
16
{
16
17
var config = new ConfigurationBuilder ( ) . AddJsonString ( jsonString ) . Build ( ) ;
17
18
return new LoggerConfiguration ( )
@@ -197,5 +198,197 @@ public void SinksAreConfiguredWithStaticMember()
197
198
198
199
Assert . Equal ( ConsoleThemes . Theme1 , DummyConsoleSink . Theme ) ;
199
200
}
201
+
202
+
203
+ [ Theory ]
204
+ [ InlineData ( "$switchName" , true ) ]
205
+ [ InlineData ( "$SwitchName" , true ) ]
206
+ [ InlineData ( "$switch1" , true ) ]
207
+ [ InlineData ( "$sw1tch0" , true ) ]
208
+ [ InlineData ( "$SWITCHNAME" , true ) ]
209
+ [ InlineData ( "$$switchname" , false ) ]
210
+ [ InlineData ( "$switchname$" , false ) ]
211
+ [ InlineData ( "switch$name" , false ) ]
212
+ [ InlineData ( "$" , false ) ]
213
+ [ InlineData ( "" , false ) ]
214
+ [ InlineData ( " " , false ) ]
215
+ [ InlineData ( "$1switch" , false ) ]
216
+ [ InlineData ( "$switch_name" , false ) ]
217
+ public void LoggingLevelSwitchNameValidityScenarios ( string switchName , bool expectedValid )
218
+ {
219
+ Assert . True ( ConfigurationReader . IsValidSwitchName ( switchName ) == expectedValid ,
220
+ $ "expected IsValidSwitchName({ switchName } ) to return { expectedValid } ") ;
221
+ }
222
+
223
+ [ Fact ]
224
+ public void LoggingLevelSwitchWithInvalidNameThrowsFormatException ( )
225
+ {
226
+ var json = @"{
227
+ ""Serilog"": {
228
+ ""LevelSwitches"": {""switchNameNotStartingWithDollar"" : ""Warning"" }
229
+ }
230
+ }" ;
231
+
232
+ var ex = Assert . Throws < FormatException > ( ( ) => ConfigFromJson ( json ) ) ;
233
+
234
+ Assert . Contains ( "\" switchNameNotStartingWithDollar\" " , ex . Message ) ;
235
+ Assert . Contains ( "'$' sign" , ex . Message ) ;
236
+ Assert . Contains ( "\" LevelSwitches\" : {\" $switchName\" :" , ex . Message ) ;
237
+ }
238
+
239
+ [ Fact ]
240
+ public void LoggingLevelSwitchIsConfigured ( )
241
+ {
242
+ var json = @"{
243
+ ""Serilog"": {
244
+ ""LevelSwitches"": {""$switch1"" : ""Warning"" },
245
+ ""MinimumLevel"" : {
246
+ ""ControlledBy"" : ""$switch1""
247
+ }
248
+ }
249
+ }" ;
250
+ LogEvent evt = null ;
251
+
252
+ var log = ConfigFromJson ( json )
253
+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
254
+ . CreateLogger ( ) ;
255
+
256
+ log . Write ( Some . DebugEvent ( ) ) ;
257
+ Assert . True ( evt is null , "LoggingLevelSwitch initial level was Warning. It should not log Debug messages" ) ;
258
+ log . Write ( Some . InformationEvent ( ) ) ;
259
+ Assert . True ( evt is null , "LoggingLevelSwitch initial level was Warning. It should not log Information messages" ) ;
260
+ log . Write ( Some . WarningEvent ( ) ) ;
261
+ Assert . True ( evt != null , "LoggingLevelSwitch initial level was Warning. It should log Warning messages" ) ;
262
+ }
263
+
264
+ [ Fact ]
265
+ public void SettingMinimumLevelControlledByToAnUndeclaredSwitchThrows ( )
266
+ {
267
+ var json = @"{
268
+ ""Serilog"": {
269
+ ""LevelSwitches"": {""$switch1"" : ""Warning"" },
270
+ ""MinimumLevel"" : {
271
+ ""ControlledBy"" : ""$switch2""
272
+ }
273
+ }
274
+ }" ;
275
+
276
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) =>
277
+ ConfigFromJson ( json )
278
+ . CreateLogger ( ) ) ;
279
+
280
+ Assert . Contains ( "$switch2" , ex . Message ) ;
281
+ Assert . Contains ( "\" LevelSwitches\" :{\" $switch2\" :" , ex . Message ) ;
282
+ }
283
+
284
+ [ Fact ]
285
+ public void LoggingLevelSwitchIsPassedToSinks ( )
286
+ {
287
+ var json = @"{
288
+ ""Serilog"": {
289
+ ""Using"": [""TestDummies""],
290
+ ""LevelSwitches"": {""$switch1"" : ""Information"" },
291
+ ""MinimumLevel"" : {
292
+ ""ControlledBy"" : ""$switch1""
293
+ },
294
+ ""WriteTo"": [{
295
+ ""Name"": ""DummyWithLevelSwitch"",
296
+ ""Args"": {""controlLevelSwitch"" : ""$switch1""}
297
+ }]
298
+ }
299
+ }" ;
300
+
301
+ LogEvent evt = null ;
302
+
303
+ var log = ConfigFromJson ( json )
304
+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
305
+ . CreateLogger ( ) ;
306
+
307
+ Assert . False ( DummyWithLevelSwitchSink . ControlLevelSwitch == null , "Sink ControlLevelSwitch should have been initialized" ) ;
308
+
309
+ var controlSwitch = DummyWithLevelSwitchSink . ControlLevelSwitch ;
310
+ Assert . NotNull ( controlSwitch ) ;
311
+
312
+ log . Write ( Some . DebugEvent ( ) ) ;
313
+ Assert . True ( evt is null , "LoggingLevelSwitch initial level was information. It should not log Debug messages" ) ;
314
+
315
+ controlSwitch . MinimumLevel = LogEventLevel . Debug ;
316
+ log . Write ( Some . DebugEvent ( ) ) ;
317
+ Assert . True ( evt != null , "LoggingLevelSwitch level was changed to Debug. It should log Debug messages" ) ;
318
+ }
319
+
320
+ [ Fact ]
321
+ public void ReferencingAnUndeclaredSwitchInSinkThrows ( )
322
+ {
323
+ var json = @"{
324
+ ""Serilog"": {
325
+ ""Using"": [""TestDummies""],
326
+ ""LevelSwitches"": {""$switch1"" : ""Information"" },
327
+ ""MinimumLevel"" : {
328
+ ""ControlledBy"" : ""$switch1""
329
+ },
330
+ ""WriteTo"": [{
331
+ ""Name"": ""DummyWithLevelSwitch"",
332
+ ""Args"": {""controlLevelSwitch"" : ""$switch2""}
333
+ }]
334
+ }
335
+ }" ;
336
+
337
+ var ex = Assert . Throws < InvalidOperationException > ( ( ) =>
338
+ ConfigFromJson ( json )
339
+ . CreateLogger ( ) ) ;
340
+
341
+ Assert . Contains ( "$switch2" , ex . Message ) ;
342
+ Assert . Contains ( "\" LevelSwitches\" :{\" $switch2\" :" , ex . Message ) ;
343
+ }
344
+
345
+ [ Fact ]
346
+ public void LoggingLevelSwitchCanBeUsedForMinimumLevelOverrides ( )
347
+ {
348
+ var json = @"{
349
+ ""Serilog"": {
350
+ ""Using"": [""TestDummies""],
351
+ ""LevelSwitches"": {""$specificSwitch"" : ""Warning"" },
352
+ ""MinimumLevel"" : {
353
+ ""Default"" : ""Debug"",
354
+ ""Override"" : {
355
+ ""System"" : ""$specificSwitch""
356
+ }
357
+ },
358
+ ""WriteTo"": [{
359
+ ""Name"": ""DummyWithLevelSwitch"",
360
+ ""Args"": {""controlLevelSwitch"" : ""$specificSwitch""}
361
+ }]
362
+ }
363
+ }" ;
364
+
365
+ LogEvent evt = null ;
366
+
367
+ var log = ConfigFromJson ( json )
368
+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
369
+ . CreateLogger ( ) ;
370
+
371
+ var systemLogger = log . ForContext ( Constants . SourceContextPropertyName , "System.Bar" ) ;
372
+
373
+ log . Write ( Some . InformationEvent ( ) ) ;
374
+ Assert . False ( evt is null , "Minimum level is Debug. It should log Information messages" ) ;
375
+
376
+ evt = null ;
377
+ // ReSharper disable HeuristicUnreachableCode
378
+ systemLogger . Write ( Some . InformationEvent ( ) ) ;
379
+ Assert . True ( evt is null , "LoggingLevelSwitch initial level was Warning for logger System.*. It should not log Information messages for SourceContext System.Bar" ) ;
380
+
381
+ systemLogger . Write ( Some . WarningEvent ( ) ) ;
382
+ Assert . False ( evt is null , "LoggingLevelSwitch initial level was Warning for logger System.*. It should log Warning messages for SourceContext System.Bar" ) ;
383
+
384
+
385
+ evt = null ;
386
+ var controlSwitch = DummyWithLevelSwitchSink . ControlLevelSwitch ;
387
+
388
+ controlSwitch . MinimumLevel = LogEventLevel . Information ;
389
+ systemLogger . Write ( Some . InformationEvent ( ) ) ;
390
+ Assert . False ( evt is null , "LoggingLevelSwitch level was changed to Information for logger System.*. It should now log Information events for SourceContext System.Bar." ) ;
391
+ // ReSharper restore HeuristicUnreachableCode
392
+ }
200
393
}
201
394
}
0 commit comments