Skip to content

Commit ee7e348

Browse files
committed
Support binary data type, support specify data length in column config, support specify allow null column.
1 parent a6fc78e commit ee7e348

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
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)|(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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
142142
case "bigint":
143143
dataType = Type.GetType("System.Int64");
144144
break;
145+
case "binary":
146+
dataType = Type.GetType("System.Byte[]");
147+
column.ExtendedProperties["DataLength"] = c.DataLength;
148+
break;
145149
case "bit":
146150
dataType = Type.GetType("System.Boolean");
147151
break;
@@ -152,6 +156,7 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
152156
case "text":
153157
case "varchar":
154158
dataType = Type.GetType("System.String");
159+
column.MaxLength = c.DataLength;
155160
break;
156161
case "date":
157162
case "datetime":
@@ -184,11 +189,14 @@ private static void GenerateDataColumnsFromConfig(MSSqlServerConfigurationSectio
184189
dataType = Type.GetType("System.Guid");
185190
break;
186191
}
192+
187193
column.DataType = dataType;
194+
column.AllowDBNull = c.AllowNull;
188195
if (columnOptions.AdditionalDataColumns == null)
189196
{
190197
columnOptions.AdditionalDataColumns = new Collection<DataColumn>();
191198
}
199+
192200
columnOptions.AdditionalDataColumns.Add(column);
193201
}
194202
}

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

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

104+
case "System.Byte[]":
105+
sqlType = columnSize == -1 ? "VARBINARY" : "BINARY(" + columnSize.ToString() + ")";
106+
break;
107+
104108
case "System.String":
105109
sqlType = "NVARCHAR(" + ((columnSize == -1) ? "MAX" : columnSize.ToString()) + ")";
106110
break;
@@ -148,7 +152,13 @@ private static string SqlGetType(object type, int columnSize, int numericPrecisi
148152
// Overload based on DataColumn from DataTable type
149153
private static string SqlGetType(DataColumn column)
150154
{
151-
return SqlGetType(column.DataType, column.MaxLength, 10, 2, column.AllowDBNull);
155+
int dataLength = -1;
156+
if (!column.ExtendedProperties.ContainsKey("DataLength") || !int.TryParse(column.ExtendedProperties["DataLength"].ToString(), out dataLength))
157+
{
158+
dataLength = column.MaxLength;
159+
}
160+
161+
return SqlGetType(column.DataType, dataLength, 10, 2, column.AllowDBNull);
152162
}
153163

154164
#endregion

0 commit comments

Comments
 (0)