Skip to content

Commit 6ea14d7

Browse files
rocknetcrockgmo
authored andcommitted
Allow specifying a PropertyName for custom columns, rather than requiring
1 parent 36b6bea commit 6ea14d7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ public virtual string ColumnName
4040
set { this[nameof(ColumnName)] = value; }
4141
}
4242

43+
[ConfigurationProperty("PropertyName")]
44+
public virtual string PropertyName
45+
{
46+
get { return (string)this["PropertyName"]; }
47+
set { this["PropertyName"] = value; }
48+
}
49+
4350
[ConfigurationProperty("DataType")]
4451
public string DataType
4552
{
@@ -75,6 +82,8 @@ internal SqlColumn AsSqlColumn()
7582
// inheritors can override IsRequired; config might not change the names of Standard Columns
7683
SetProperty.IfProvidedNotEmpty<string>(this, nameof(ColumnName), (val) => sqlColumn.ColumnName = val);
7784

85+
SetProperty.IfProvidedNotEmpty<string>(this, nameof(PropertyName), (val) => sqlColumn.PropertyName = val);
86+
7887
SetProperty.IfProvidedNotEmpty<string>(this, nameof(DataType), (val) => sqlColumn.SetDataTypeFromConfigString(val));
7988

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

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/MSSqlServerSinkTraits.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,14 @@ private IEnumerable<KeyValuePair<string, object>> ConvertPropertiesToColumn(IRea
229229
{
230230
foreach (var property in properties)
231231
{
232-
if (!EventTable.Columns.Contains(property.Key) || StandardColumnNames.Contains(property.Key))
232+
var additionalColumn = columnOptions
233+
.AdditionalColumns
234+
.FirstOrDefault(ac => ac.PropertyName == property.Key);
235+
236+
if (additionalColumn == null || standardColumnNames.Contains(property.Key))
233237
continue;
234238

235-
var columnName = property.Key;
239+
var columnName = additionalColumn.ColumnName;
236240
var columnType = EventTable.Columns[columnName].DataType;
237241

238242
if (!(property.Value is ScalarValue scalarValue))

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class SqlColumn
1010
{
1111
private SqlDbType _dataType = SqlDbType.VarChar; // backwards-compatibility default
1212
private string _columnName = string.Empty;
13+
private string _propertyName;
1314

1415
/// <summary>
1516
/// Default constructor.
@@ -95,6 +96,16 @@ public SqlDbType DataType
9596
/// </summary>
9697
public bool NonClusteredIndex { get; set; } = false;
9798

99+
/// <summary>
100+
/// The name of the Serilog property to use as the value when filling the DataTable.
101+
/// If not specified, the ColumnName and PropertyName are the same.
102+
/// </summary>
103+
public string PropertyName
104+
{
105+
get => _propertyName ?? ColumnName;
106+
set => _propertyName = value;
107+
}
108+
98109
// Set by the constructors of the Standard Column classes that inherit from this;
99110
// allows Standard Columns and user-defined columns to coexist but remain identifiable
100111
// and allows casting back to the Standard Column without a lot of switch gymnastics.
@@ -128,7 +139,7 @@ internal virtual DataColumn AsDataColumn()
128139
}
129140

130141
/// <summary>
131-
/// Configuration accepts DataType as a simple string ("nvarchar" for example) for ease-of-use.
142+
/// Configuration accepts DataType as a simple string ("nvarchar" for example) for ease-of-use.
132143
/// This converts to SqlDbType and stores it into the DataType property.
133144
/// </summary>
134145
internal void SetDataTypeFromConfigString(string requestedSqlType)

0 commit comments

Comments
 (0)