@@ -123,46 +123,51 @@ void ApplyEnrichment(LoggerConfiguration loggerConfiguration)
123
123
}
124
124
}
125
125
126
- Dictionary < string , Dictionary < string , IConfigurationArgumentValue > > GetMethodCalls ( IConfigurationSection directive )
126
+ internal ILookup < string , Dictionary < string , IConfigurationArgumentValue > > GetMethodCalls ( IConfigurationSection directive )
127
127
{
128
- var result = new Dictionary < string , Dictionary < string , IConfigurationArgumentValue > > ( ) ;
129
- foreach ( var child in directive . GetChildren ( ) )
128
+ var children = directive . GetChildren ( ) ;
129
+
130
+ var result =
131
+ ( from child in children
132
+ where child . Value != null // Plain string
133
+ select new { Name = child . Value , Args = new Dictionary < string , IConfigurationArgumentValue > ( ) } )
134
+ . Concat (
135
+ ( from child in children
136
+ where child . Value == null
137
+ let name = GetSectionName ( child )
138
+ let callArgs = ( from argument in child . GetSection ( "Args" ) . GetChildren ( )
139
+ select new {
140
+ Name = argument . Key ,
141
+ Value = GetArgumentValue ( argument ) } ) . ToDictionary ( p => p . Name , p => p . Value )
142
+ select new { Name = name , Args = callArgs } ) )
143
+ . ToLookup ( p => p . Name , p => p . Args ) ;
144
+
145
+ return result ;
146
+
147
+ IConfigurationArgumentValue GetArgumentValue ( IConfigurationSection argumentSection )
130
148
{
131
- if ( child . Value != null )
149
+ IConfigurationArgumentValue argumentValue ;
150
+ if ( argumentSection . Value != null )
132
151
{
133
- // Plain string
134
- result . Add ( child . Value , new Dictionary < string , IConfigurationArgumentValue > ( ) ) ;
152
+ argumentValue = new StringArgumentValue ( ( ) => argumentSection . Value , argumentSection . GetReloadToken ) ;
135
153
}
136
154
else
137
155
{
138
- var name = child . GetSection ( "Name" ) ;
139
- if ( name . Value == null )
140
- throw new InvalidOperationException ( $ "The configuration value in { name . Path } has no Name element.") ;
141
-
142
- var callArgs = new Dictionary < string , IConfigurationArgumentValue > ( ) ;
143
- var args = child . GetSection ( "Args" ) ;
144
- if ( args != null )
145
- {
146
- foreach ( var argument in args . GetChildren ( ) )
147
- {
148
- IConfigurationArgumentValue argumentValue ;
149
- if ( argument . Value != null )
150
- {
151
- argumentValue = new StringArgumentValue ( ( ) => argument . Value , argument . GetReloadToken ) ;
152
- }
153
- else
154
- {
155
- argumentValue = new ConfigurationSectionArgumentValue ( new ConfigurationReader ( argument , _configurationAssemblies , _dependencyContext ) ) ;
156
- }
157
-
158
- callArgs . Add ( argument . Key , argumentValue ) ;
159
- }
160
- }
161
- result . Add ( name . Value , callArgs ) ;
156
+ argumentValue = new ConfigurationSectionArgumentValue ( new ConfigurationReader ( argumentSection , _configurationAssemblies , _dependencyContext ) ) ;
162
157
}
158
+
159
+ return argumentValue ;
163
160
}
164
- return result ;
165
- }
161
+
162
+ string GetSectionName ( IConfigurationSection s )
163
+ {
164
+ var name = s . GetSection ( "Name" ) ;
165
+ if ( name . Value == null )
166
+ throw new InvalidOperationException ( $ "The configuration value in { name . Path } has no 'Name' element.") ;
167
+
168
+ return name . Value ;
169
+ }
170
+ }
166
171
167
172
Assembly [ ] LoadConfigurationAssemblies ( )
168
173
{
@@ -178,7 +183,8 @@ Assembly[] LoadConfigurationAssemblies()
178
183
"A zero-length or whitespace assembly name was supplied to a Serilog.Using configuration statement." ) ;
179
184
180
185
var assembly = Assembly . Load ( new AssemblyName ( simpleName ) ) ;
181
- assemblies . Add ( assembly . FullName , assembly ) ;
186
+ if ( ! assemblies . ContainsKey ( assembly . FullName ) )
187
+ assemblies . Add ( assembly . FullName , assembly ) ;
182
188
}
183
189
}
184
190
@@ -217,9 +223,9 @@ where filter(assemblyFileName)
217
223
return query . ToArray ( ) ;
218
224
}
219
225
220
- static void CallConfigurationMethods ( Dictionary < string , Dictionary < string , IConfigurationArgumentValue > > methods , IList < MethodInfo > configurationMethods , object receiver )
226
+ static void CallConfigurationMethods ( ILookup < string , Dictionary < string , IConfigurationArgumentValue > > methods , IList < MethodInfo > configurationMethods , object receiver )
221
227
{
222
- foreach ( var method in methods )
228
+ foreach ( var method in methods . SelectMany ( g => g . Select ( x => new { g . Key , Value = x } ) ) )
223
229
{
224
230
var methodInfo = SelectConfigurationMethod ( configurationMethods , method . Key , method . Value ) ;
225
231
0 commit comments