Skip to content

Commit 48be22c

Browse files
authored
Merge pull request #117 from quinvit/dev-extend-column-data-type
Extend column definition in config
2 parents f0a0511 + 7538e28 commit 48be22c

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,31 @@ public string ColumnName
5252
/// Type of column as it exists in SQL Server
5353
/// </summary>
5454
[ConfigurationProperty("DataType", IsRequired = true, IsKey = false, DefaultValue ="varchar")]
55-
[RegexStringValidator("(bigint)|(bit)|(char)|(date)|(datetime)|(datetime2)|(decimal)|(float)|(int)|(money)|(nchar)|(ntext)|(numeric)|(nvarchar)|(real)|(smalldatetime)|(smallint)|(smallmoney)|(text)|(time)|(uniqueidentifier)|(varchar)")]
55+
[RegexStringValidator("(bigint)|(bit)|(binary)|(varbinary)|(char)|(date)|(datetime)|(datetime2)|(decimal)|(float)|(int)|(money)|(nchar)|(ntext)|(numeric)|(nvarchar)|(real)|(smalldatetime)|(smallint)|(smallmoney)|(text)|(time)|(uniqueidentifier)|(varchar)")]
5656
public string DataType
5757
{
5858
get { return (string)this["DataType"]; }
5959
set { this["DataType"] = value; }
6060
}
61+
62+
/// <summary>
63+
/// Length of column as it exists in SQL Server for string or binary data type.
64+
/// </summary>
65+
[ConfigurationProperty("DataLength", IsRequired = false, IsKey = false, DefaultValue = 128)]
66+
public int DataLength
67+
{
68+
get { return (int)this["DataLength"]; }
69+
set { this["DataLength"] = value; }
70+
}
71+
72+
/// <summary>
73+
/// Allow nullable column as it exists in SQL Server.
74+
/// </summary>
75+
[ConfigurationProperty("AllowNull", IsRequired = false, IsKey = false, DefaultValue = true)]
76+
public bool AllowNull
77+
{
78+
get { return (bool)this["AllowNull"]; }
79+
set { this["AllowNull"] = value; }
80+
}
6181
}
6282
}

src/Serilog.Sinks.MSSqlServer/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
190190
case "bigint":
191191
dataType = typeof(long);
192192
break;
193+
case "varbinary":
194+
case "binary":
195+
dataType = Type.GetType("System.Byte[]");
196+
column.ExtendedProperties["DataLength"] = c.DataLength;
197+
break;
193198
case "bit":
194199
dataType = typeof(bool);
195200
break;
@@ -199,7 +204,8 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
199204
case "nvarchar":
200205
case "text":
201206
case "varchar":
202-
dataType = typeof(string);
207+
dataType = Type.GetType("System.String");
208+
column.MaxLength = c.DataLength;
203209
break;
204210
case "date":
205211
case "datetime":
@@ -232,11 +238,14 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
232238
dataType = typeof(Guid);
233239
break;
234240
}
241+
235242
column.DataType = dataType;
243+
column.AllowDBNull = c.AllowNull;
236244
if (columnOptions.AdditionalDataColumns == null)
237245
{
238246
columnOptions.AdditionalDataColumns = new Collection<DataColumn>();
239247
}
248+
240249
columnOptions.AdditionalDataColumns.Add(column);
241250
}
242251
}

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/SqlTableCreator.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ private static string SqlGetType(object type, int columnSize, int numericPrecisi
101101
sqlType = "TINYINT";
102102
break;
103103

104-
case "System.String":
105-
sqlType = "NVARCHAR(" + ((columnSize == -1) ? "MAX" : columnSize.ToString()) + ")";
104+
case "System.Byte[]":
105+
sqlType = columnSize == -1 ? "VARBINARY(MAX)" : "VARBINARY(" + columnSize.ToString() + ")";
106106
break;
107107

108+
case "System.String":
109+
sqlType = "NVARCHAR(" + ((columnSize == -1) ? "MAX" : columnSize.ToString()) + ")";
110+
break;
111+
108112
case "System.Decimal":
109113
if (numericScale > 0)
110114
sqlType = "REAL";
@@ -153,4 +157,5 @@ private static string SqlGetType(DataColumn column)
153157

154158
#endregion
155159
}
160+
156161
}

0 commit comments

Comments
 (0)