Skip to content

Commit

Permalink
Force nonhierachical property: sink logic
Browse files Browse the repository at this point in the history
* Disable a property from being resolved hierarchically/structured even if its name contains dots (serilog-mssql#542) be addin a new new column option `ResolveHierarchicalPropertyName`.
* Implemented logic in SqlColumn class.
* Implemented reading of new column option from config.

Issue serilog-mssql#542
  • Loading branch information
ckadluba committed Nov 20, 2024
1 parent acfc442 commit 8a32830
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2015 Serilog Contributors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -30,6 +30,12 @@ protected override object GetElementKey(ConfigurationElement element)
{
return ((ColumnConfig)element)?.ColumnName;
}

// For testing
internal void Add(ColumnConfig config)
{
BaseAdd(config);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright 2015 Serilog Contributors
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -48,6 +48,13 @@ public virtual string PropertyName
set { this[nameof(PropertyName)] = value; }
}

[ConfigurationProperty("ResolveHierarchicalPropertyName")]
public virtual string ResolveHierarchicalPropertyName
{
get { return (string)this[nameof(ResolveHierarchicalPropertyName)]; }
set { this[nameof(ResolveHierarchicalPropertyName)] = value; }
}

[ConfigurationProperty("DataType")]
public string DataType
{
Expand Down Expand Up @@ -85,6 +92,8 @@ internal SqlColumn AsSqlColumn()

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

SetProperty.IfProvided<bool>(this, nameof(ResolveHierarchicalPropertyName), (val) => sqlColumn.ResolveHierarchicalPropertyName = val);

SetProperty.IfProvidedNotEmpty<string>(this, nameof(DataType), (val) => sqlColumn.SetDataTypeFromConfigString(val));

SetProperty.IfProvided<int>(this, nameof(DataLength), (val) => sqlColumn.DataLength = val);
Expand Down
30 changes: 25 additions & 5 deletions src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class SqlColumn
private SqlDbType _dataType = SqlDbType.VarChar; // backwards-compatibility default
private string _columnName = string.Empty;
private string _propertyName;
private bool _resolveHierarchicalPropertyName = true;
private readonly List<string> _propertyNameHierarchy = new List<string>();

/// <summary>
Expand Down Expand Up @@ -109,7 +110,20 @@ public string PropertyName
set
{
_propertyName = value;
ParseHierarchicalPropertyName(value);
ParseHierarchicalPropertyName();
}
}

/// <summary>
/// Controls whether hierarchical expressions in `PropertyName` are evaluated. The default is `true`.
/// </summary>
public bool ResolveHierarchicalPropertyName
{
get => _resolveHierarchicalPropertyName;
set
{
_resolveHierarchicalPropertyName = value;
ParseHierarchicalPropertyName();
}
}

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

internal Type StandardColumnType { get; set; }

/// <summary>
/// Converts a SQL sink SqlColumn object to a System.Data.DataColumn object. The original
/// SqlColumn object is stored in the DataColumn's ExtendedProperties collection.
Expand Down Expand Up @@ -170,9 +182,17 @@ internal void SetDataTypeFromConfigString(string requestedSqlType)
DataType = sqlType;
}

private void ParseHierarchicalPropertyName(string propertyName)
private void ParseHierarchicalPropertyName()
{
_propertyNameHierarchy.AddRange(propertyName.Split('.'));
_propertyNameHierarchy.Clear();
if (ResolveHierarchicalPropertyName)
{
_propertyNameHierarchy.AddRange(PropertyName.Split('.'));
}
else
{
_propertyNameHierarchy.Add(PropertyName);
}
}
}
}

0 comments on commit 8a32830

Please sign in to comment.