1
- using System ;
1
+ using System ;
2
2
using Microsoft . Extensions . Configuration ;
3
+ using Serilog . Events ;
4
+ using Serilog . Settings . Configuration . Tests . Support ;
3
5
using Xunit ;
4
6
5
7
namespace Serilog . Settings . Configuration . Tests
@@ -14,5 +16,89 @@ public void ReadFromConfigurationShouldNotThrowOnEmptyConfiguration()
14
16
// should not throw
15
17
act ( ) ;
16
18
}
19
+
20
+ [ Fact ]
21
+ [ Trait ( "BugFix" , "https://github.com/serilog/serilog-settings-configuration/issues/143" ) ]
22
+ public void ReadFromConfigurationSectionReadsFromAnArbitrarySection ( )
23
+ {
24
+ LogEvent evt = null ;
25
+
26
+ var json = @"{
27
+ ""NotSerilog"": {
28
+ ""Properties"": {
29
+ ""App"": ""Test""
30
+ }
31
+ }
32
+ }" ;
33
+
34
+ var config = new ConfigurationBuilder ( )
35
+ . AddJsonString ( json )
36
+ . Build ( ) ;
37
+
38
+ var log = new LoggerConfiguration ( )
39
+ . ReadFrom . ConfigurationSection ( config . GetSection ( "NotSerilog" ) )
40
+ . WriteTo . Sink ( new DelegatingSink ( e => evt = e ) )
41
+ . CreateLogger ( ) ;
42
+
43
+ log . Information ( "Has a test property" ) ;
44
+
45
+ Assert . NotNull ( evt ) ;
46
+ Assert . Equal ( "Test" , evt . Properties [ "App" ] . LiteralValue ( ) ) ;
47
+ }
48
+
49
+ [ Fact ( Skip = "Passes when run alone, but fails when the whole suite is run - to fix" ) ]
50
+ [ Trait ( "BugFix" , "https://github.com/serilog/serilog-settings-configuration/issues/143" ) ]
51
+ public void ReadFromConfigurationSectionThrowsWhenTryingToCallConfigurationMethodWithIConfigurationParam ( )
52
+ {
53
+ var json = @"{
54
+ ""NotSerilog"": {
55
+ ""Using"": [""TestDummies""],
56
+ ""WriteTo"": [{
57
+ ""Name"": ""DummyWithConfiguration"",
58
+ ""Args"": {}
59
+ }]
60
+ }
61
+ }" ;
62
+
63
+ var config = new ConfigurationBuilder ( )
64
+ . AddJsonString ( json )
65
+ . Build ( ) ;
66
+
67
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) =>
68
+ new LoggerConfiguration ( )
69
+ . ReadFrom . ConfigurationSection ( config . GetSection ( "NotSerilog" ) )
70
+ . CreateLogger ( ) ) ;
71
+
72
+ Assert . Equal ( "Trying to invoke a configuration method accepting a `IConfiguration` argument. " +
73
+ "This is not supported when only a `IConfigSection` has been provided. " +
74
+ "(method 'Serilog.LoggerConfiguration DummyWithConfiguration(Serilog.Configuration.LoggerSinkConfiguration, Microsoft.Extensions.Configuration.IConfiguration, Microsoft.Extensions.Configuration.IConfigurationSection, System.String, Serilog.Events.LogEventLevel)')" ,
75
+ exception . Message ) ;
76
+
77
+ }
78
+
79
+ [ Fact ]
80
+ [ Trait ( "BugFix" , "https://github.com/serilog/serilog-settings-configuration/issues/143" ) ]
81
+ public void ReadFromConfigurationSectionDoesNotThrowWhenTryingToCallConfigurationMethodWithOptionalIConfigurationParam ( )
82
+ {
83
+ var json = @"{
84
+ ""NotSerilog"": {
85
+ ""Using"": [""TestDummies""],
86
+ ""WriteTo"": [{
87
+ ""Name"": ""DummyWithOptionalConfiguration"",
88
+ ""Args"": {}
89
+ }]
90
+ }
91
+ }" ;
92
+
93
+ var config = new ConfigurationBuilder ( )
94
+ . AddJsonString ( json )
95
+ . Build ( ) ;
96
+
97
+ // this should not throw because DummyWithOptionalConfiguration accepts an optional config
98
+ new LoggerConfiguration ( )
99
+ . ReadFrom . ConfigurationSection ( config . GetSection ( "NotSerilog" ) )
100
+ . CreateLogger ( ) ;
101
+
102
+ }
17
103
}
18
104
}
0 commit comments