Skip to content

Commit 8a32830

Browse files
committed
Force nonhierachical property: sink logic
* Disable a property from being resolved hierarchically/structured even if its name contains dots (#542) be addin a new new column option `ResolveHierarchicalPropertyName`. * Implemented logic in SqlColumn class. * Implemented reading of new column option from config. Issue #542
1 parent acfc442 commit 8a32830

File tree

3 files changed

+46
-11
lines changed

3 files changed

+46
-11
lines changed

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/ColumnCollection.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2015 Serilog Contributors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -30,6 +30,12 @@ protected override object GetElementKey(ConfigurationElement element)
3030
{
3131
return ((ColumnConfig)element)?.ColumnName;
3232
}
33+
34+
// For testing
35+
internal void Add(ColumnConfig config)
36+
{
37+
BaseAdd(config);
38+
}
3339
}
3440
}
3541

src/Serilog.Sinks.MSSqlServer/Configuration/Implementations/System.Configuration/ColumnConfig.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright 2015 Serilog Contributors
2-
//
2+
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
55
// You may obtain a copy of the License at
6-
//
6+
//
77
// http://www.apache.org/licenses/LICENSE-2.0
8-
//
8+
//
99
// Unless required by applicable law or agreed to in writing, software
1010
// distributed under the License is distributed on an "AS IS" BASIS,
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -48,6 +48,13 @@ public virtual string PropertyName
4848
set { this[nameof(PropertyName)] = value; }
4949
}
5050

51+
[ConfigurationProperty("ResolveHierarchicalPropertyName")]
52+
public virtual string ResolveHierarchicalPropertyName
53+
{
54+
get { return (string)this[nameof(ResolveHierarchicalPropertyName)]; }
55+
set { this[nameof(ResolveHierarchicalPropertyName)] = value; }
56+
}
57+
5158
[ConfigurationProperty("DataType")]
5259
public string DataType
5360
{
@@ -85,6 +92,8 @@ internal SqlColumn AsSqlColumn()
8592

8693
SetProperty.IfProvidedNotEmpty<string>(this, nameof(PropertyName), (val) => sqlColumn.PropertyName = val);
8794

95+
SetProperty.IfProvided<bool>(this, nameof(ResolveHierarchicalPropertyName), (val) => sqlColumn.ResolveHierarchicalPropertyName = val);
96+
8897
SetProperty.IfProvidedNotEmpty<string>(this, nameof(DataType), (val) => sqlColumn.SetDataTypeFromConfigString(val));
8998

9099
SetProperty.IfProvided<int>(this, nameof(DataLength), (val) => sqlColumn.DataLength = val);

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class SqlColumn
1313
private SqlDbType _dataType = SqlDbType.VarChar; // backwards-compatibility default
1414
private string _columnName = string.Empty;
1515
private string _propertyName;
16+
private bool _resolveHierarchicalPropertyName = true;
1617
private readonly List<string> _propertyNameHierarchy = new List<string>();
1718

1819
/// <summary>
@@ -109,7 +110,20 @@ public string PropertyName
109110
set
110111
{
111112
_propertyName = value;
112-
ParseHierarchicalPropertyName(value);
113+
ParseHierarchicalPropertyName();
114+
}
115+
}
116+
117+
/// <summary>
118+
/// Controls whether hierarchical expressions in `PropertyName` are evaluated. The default is `true`.
119+
/// </summary>
120+
public bool ResolveHierarchicalPropertyName
121+
{
122+
get => _resolveHierarchicalPropertyName;
123+
set
124+
{
125+
_resolveHierarchicalPropertyName = value;
126+
ParseHierarchicalPropertyName();
113127
}
114128
}
115129

@@ -130,8 +144,6 @@ internal bool HasHierarchicalPropertyName
130144
// and allows casting back to the Standard Column without a lot of switch gymnastics.
131145
internal StandardColumn? StandardColumnIdentifier { get; set; }
132146

133-
internal Type StandardColumnType { get; set; }
134-
135147
/// <summary>
136148
/// Converts a SQL sink SqlColumn object to a System.Data.DataColumn object. The original
137149
/// SqlColumn object is stored in the DataColumn's ExtendedProperties collection.
@@ -170,9 +182,17 @@ internal void SetDataTypeFromConfigString(string requestedSqlType)
170182
DataType = sqlType;
171183
}
172184

173-
private void ParseHierarchicalPropertyName(string propertyName)
185+
private void ParseHierarchicalPropertyName()
174186
{
175-
_propertyNameHierarchy.AddRange(propertyName.Split('.'));
187+
_propertyNameHierarchy.Clear();
188+
if (ResolveHierarchicalPropertyName)
189+
{
190+
_propertyNameHierarchy.AddRange(PropertyName.Split('.'));
191+
}
192+
else
193+
{
194+
_propertyNameHierarchy.Add(PropertyName);
195+
}
176196
}
177197
}
178198
}

0 commit comments

Comments
 (0)