@@ -151,9 +151,6 @@ private static string GetConnectionString(string nameOrConnectionString, IConfig
151
151
return cs ;
152
152
}
153
153
154
- // simulate using a property setter as an out parameter
155
- delegate void PropertySetter < T > ( T value ) ;
156
-
157
154
/// <summary>
158
155
/// Create or add to the ColumnOptions object and apply any configuration changes to it.
159
156
/// </summary>
@@ -184,7 +181,8 @@ void AddRemoveStandardColumns()
184
181
{
185
182
foreach ( var col in addStd . GetChildren ( ) . ToList ( ) )
186
183
{
187
- if ( Enum . TryParse ( col . Value , ignoreCase : true , result : out StandardColumn stdcol ) )
184
+ if ( Enum . TryParse ( col . Value , ignoreCase : true , result : out StandardColumn stdcol )
185
+ && ! opts . Store . Contains ( stdcol ) )
188
186
opts . Store . Add ( stdcol ) ;
189
187
}
190
188
}
@@ -195,7 +193,8 @@ void AddRemoveStandardColumns()
195
193
{
196
194
foreach ( var col in removeStd . GetChildren ( ) . ToList ( ) )
197
195
{
198
- if ( Enum . TryParse ( col . Value , ignoreCase : true , result : out StandardColumn stdcol ) )
196
+ if ( Enum . TryParse ( col . Value , ignoreCase : true , result : out StandardColumn stdcol )
197
+ && opts . Store . Contains ( stdcol ) )
199
198
opts . Store . Remove ( stdcol ) ;
200
199
}
201
200
}
@@ -211,12 +210,12 @@ void AddAdditionalColumns()
211
210
{
212
211
foreach ( var c in newcols )
213
212
{
214
- if ( ! string . IsNullOrWhiteSpace ( c . ColumnName ) ) // && !string.IsNullOrWhiteSpace(c.DataType))
213
+ if ( ! string . IsNullOrWhiteSpace ( c . ColumnName ) )
215
214
{
216
215
if ( opts . AdditionalColumns == null )
217
216
opts . AdditionalColumns = new Collection < SqlColumn > ( ) ;
218
217
219
- opts . AdditionalColumns . Add ( c ) ; //.AsSqlColumn());
218
+ opts . AdditionalColumns . Add ( c ) ;
220
219
}
221
220
}
222
221
}
@@ -229,49 +228,49 @@ void ReadStandardColumns()
229
228
{
230
229
SetCommonColumnOptions ( opts . Id ) ;
231
230
#pragma warning disable 618 // deprecated: BigInt property
232
- SetIfProvided < bool > ( ( val ) => { opts . Id . BigInt = val ; } , section [ "bigInt" ] ) ;
231
+ SetProperty . IfNotNull < bool > ( section [ "bigInt" ] , ( val ) => opts . Id . BigInt = val ) ;
233
232
#pragma warning restore 618
234
233
}
235
234
236
235
section = config . GetSection ( "level" ) ;
237
236
if ( section != null )
238
237
{
239
238
SetCommonColumnOptions ( opts . Level ) ;
240
- SetIfProvided < bool > ( ( val ) => { opts . Level . StoreAsEnum = val ; } , section [ "storeAsEnum" ] ) ;
239
+ SetProperty . IfNotNull < bool > ( section [ "storeAsEnum" ] , ( val ) => opts . Level . StoreAsEnum = val ) ;
241
240
}
242
241
243
242
section = config . GetSection ( "properties" ) ;
244
243
if ( section != null )
245
244
{
246
245
SetCommonColumnOptions ( opts . Properties ) ;
247
- SetIfProvided < bool > ( ( val ) => { opts . Properties . ExcludeAdditionalProperties = val ; } , section [ "excludeAdditionalProperties" ] ) ;
248
- SetIfProvided < string > ( ( val ) => { opts . Properties . DictionaryElementName = val ; } , section [ "dictionaryElementName" ] ) ;
249
- SetIfProvided < string > ( ( val ) => { opts . Properties . ItemElementName = val ; } , section [ "itemElementName" ] ) ;
250
- SetIfProvided < bool > ( ( val ) => { opts . Properties . OmitDictionaryContainerElement = val ; } , section [ "omitDictionaryContainerElement" ] ) ;
251
- SetIfProvided < bool > ( ( val ) => { opts . Properties . OmitSequenceContainerElement = val ; } , section [ "omitSequenceContainerElement" ] ) ;
252
- SetIfProvided < bool > ( ( val ) => { opts . Properties . OmitStructureContainerElement = val ; } , section [ "omitStructureContainerElement" ] ) ;
253
- SetIfProvided < bool > ( ( val ) => { opts . Properties . OmitElementIfEmpty = val ; } , section [ "omitElementIfEmpty" ] ) ;
254
- SetIfProvided < string > ( ( val ) => { opts . Properties . PropertyElementName = val ; } , section [ "propertyElementName" ] ) ;
255
- SetIfProvided < string > ( ( val ) => { opts . Properties . RootElementName = val ; } , section [ "rootElementName" ] ) ;
256
- SetIfProvided < string > ( ( val ) => { opts . Properties . SequenceElementName = val ; } , section [ "sequenceElementName" ] ) ;
257
- SetIfProvided < string > ( ( val ) => { opts . Properties . StructureElementName = val ; } , section [ "structureElementName" ] ) ;
258
- SetIfProvided < bool > ( ( val ) => { opts . Properties . UsePropertyKeyAsElementName = val ; } , section [ "usePropertyKeyAsElementName" ] ) ;
246
+ SetProperty . IfNotNull < bool > ( section [ "excludeAdditionalProperties" ] , ( val ) => opts . Properties . ExcludeAdditionalProperties = val ) ;
247
+ SetProperty . IfNotNull < string > ( section [ "dictionaryElementName" ] , ( val ) => opts . Properties . DictionaryElementName = val ) ;
248
+ SetProperty . IfNotNull < string > ( section [ "itemElementName" ] , ( val ) => opts . Properties . ItemElementName = val ) ;
249
+ SetProperty . IfNotNull < bool > ( section [ "omitDictionaryContainerElement" ] , ( val ) => opts . Properties . OmitDictionaryContainerElement = val ) ;
250
+ SetProperty . IfNotNull < bool > ( section [ "omitSequenceContainerElement" ] , ( val ) => opts . Properties . OmitSequenceContainerElement = val ) ;
251
+ SetProperty . IfNotNull < bool > ( section [ "omitStructureContainerElement" ] , ( val ) => opts . Properties . OmitStructureContainerElement = val ) ;
252
+ SetProperty . IfNotNull < bool > ( section [ "omitElementIfEmpty" ] , ( val ) => opts . Properties . OmitElementIfEmpty = val ) ;
253
+ SetProperty . IfNotNull < string > ( section [ "propertyElementName" ] , ( val ) => opts . Properties . PropertyElementName = val ) ;
254
+ SetProperty . IfNotNull < string > ( section [ "rootElementName" ] , ( val ) => opts . Properties . RootElementName = val ) ;
255
+ SetProperty . IfNotNull < string > ( section [ "sequenceElementName" ] , ( val ) => opts . Properties . SequenceElementName = val ) ;
256
+ SetProperty . IfNotNull < string > ( section [ "structureElementName" ] , ( val ) => opts . Properties . StructureElementName = val ) ;
257
+ SetProperty . IfNotNull < bool > ( section [ "usePropertyKeyAsElementName" ] , ( val ) => opts . Properties . UsePropertyKeyAsElementName = val ) ;
259
258
// TODO PropertiesFilter would need a compiled Predicate<string> (high Roslyn overhead, see Serilog Config repo #106)
260
259
}
261
260
262
261
section = config . GetSection ( "timeStamp" ) ;
263
262
if ( section != null )
264
263
{
265
264
SetCommonColumnOptions ( opts . TimeStamp ) ;
266
- SetIfProvided < bool > ( ( val ) => { opts . TimeStamp . ConvertToUtc = val ; } , section [ "convertToUtc" ] ) ;
265
+ SetProperty . IfNotNull < bool > ( section [ "convertToUtc" ] , ( val ) => opts . TimeStamp . ConvertToUtc = val ) ;
267
266
}
268
267
269
268
section = config . GetSection ( "logEvent" ) ;
270
269
if ( section != null )
271
270
{
272
271
SetCommonColumnOptions ( opts . LogEvent ) ;
273
- SetIfProvided < bool > ( ( val ) => { opts . LogEvent . ExcludeAdditionalProperties = val ; } , section [ "excludeAdditionalProperties" ] ) ;
274
- SetIfProvided < bool > ( ( val ) => { opts . LogEvent . ExcludeStandardColumns = val ; } , section [ "ExcludeStandardColumns" ] ) ;
272
+ SetProperty . IfNotNull < bool > ( section [ "excludeAdditionalProperties" ] , ( val ) => opts . LogEvent . ExcludeAdditionalProperties = val ) ;
273
+ SetProperty . IfNotNull < bool > ( section [ "ExcludeStandardColumns" ] , ( val ) => opts . LogEvent . ExcludeStandardColumns = val ) ;
275
274
}
276
275
277
276
section = config . GetSection ( "message" ) ;
@@ -289,18 +288,18 @@ void ReadStandardColumns()
289
288
// Standard Columns are subclasses of the SqlColumn class
290
289
void SetCommonColumnOptions ( SqlColumn target )
291
290
{
292
- SetIfProvided < string > ( ( val ) => { target . ColumnName = val ; } , section [ "columnName" ] ) ;
293
- SetIfProvided < string > ( ( val ) => { target . SetDataTypeFromConfigString ( val ) ; } , section [ "dataType" ] ) ;
294
- SetIfProvided < bool > ( ( val ) => { target . AllowNull = val ; } , section [ "allowNull" ] ) ;
295
- SetIfProvided < int > ( ( val ) => { target . DataLength = val ; } , section [ "dataLength" ] ) ;
296
- SetIfProvided < bool > ( ( val ) => { target . NonClusteredIndex = val ; } , section [ "nonClusteredIndex" ] ) ;
291
+ SetProperty . IfNotNullOrEmpty < string > ( section [ "columnName" ] , ( val ) => target . ColumnName = val ) ;
292
+ SetProperty . IfNotNull < string > ( section [ "dataType" ] , ( val ) => target . SetDataTypeFromConfigString ( val ) ) ;
293
+ SetProperty . IfNotNull < bool > ( section [ "allowNull" ] , ( val ) => target . AllowNull = val ) ;
294
+ SetProperty . IfNotNull < int > ( section [ "dataLength" ] , ( val ) => target . DataLength = val ) ;
295
+ SetProperty . IfNotNull < bool > ( section [ "nonClusteredIndex" ] , ( val ) => target . NonClusteredIndex = val ) ;
297
296
}
298
297
}
299
298
300
299
void ReadMiscColumnOptions ( )
301
300
{
302
- SetIfProvided < bool > ( ( val ) => { opts . DisableTriggers = val ; } , config [ "disableTriggers" ] ) ;
303
- SetIfProvided < bool > ( ( val ) => { opts . ClusteredColumnstoreIndex = val ; } , config [ "clusteredColumnstoreIndex" ] ) ;
301
+ SetProperty . IfNotNull < bool > ( config [ "disableTriggers" ] , ( val ) => opts . DisableTriggers = val ) ;
302
+ SetProperty . IfNotNull < bool > ( config [ "clusteredColumnstoreIndex" ] , ( val ) => opts . ClusteredColumnstoreIndex = val ) ;
304
303
305
304
string pkName = config [ "primaryKeyColumnName" ] ;
306
305
if ( ! string . IsNullOrEmpty ( pkName ) )
@@ -334,23 +333,6 @@ void ReadMiscColumnOptions()
334
333
throw new ArgumentException ( $ "Could not match the configured primary key column name \" { pkName } \" with a data column in the table.") ;
335
334
}
336
335
}
337
-
338
- // This is used to only set a column property when it is actually specified in the config.
339
- // When a value is requested from config, it returns null if that value hasn't been specified.
340
- // This also means you can't use config to set a property to null.
341
- void SetIfProvided < T > ( PropertySetter < T > setter , string value )
342
- {
343
- if ( value == null )
344
- return ;
345
- try
346
- {
347
- var setting = ( T ) Convert . ChangeType ( value , typeof ( T ) ) ;
348
- setter ( setting ) ;
349
- }
350
- // don't change the property if the conversion failed
351
- catch ( InvalidCastException ) { }
352
- catch ( OverflowException ) { }
353
- }
354
336
}
355
337
}
356
338
}
0 commit comments