Skip to content

Commit e1b375a

Browse files
committed
add size option
1 parent 59def1a commit e1b375a

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/Serilog.Sinks.SqlServer/MappingContext.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,14 @@ public DataTable GetSchemaTable()
103103
// populate rows
104104
for (int i = 0; i < Mappings.Count; i++)
105105
{
106+
var mapping = Mappings[i];
107+
106108
var rowData = new object[5];
107109
rowData[0] = i;
108-
rowData[1] = Mappings[i].ColumnName;
109-
rowData[2] = Mappings[i].ColumnType;
110-
rowData[3] = -1;
111-
rowData[4] = Mappings[i].Nullable;
110+
rowData[1] = mapping.ColumnName;
111+
rowData[2] = mapping.ColumnType;
112+
rowData[3] = mapping.Size ?? -1;
113+
rowData[4] = mapping.Nullable;
112114

113115
table.Rows.Add(rowData);
114116
}

src/Serilog.Sinks.SqlServer/SqlServerSinkOptions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class SqlServerSinkOptions : BatchingOptions
5959
/// <param name="propertyName">The name of the log event property to map.</param>
6060
/// <param name="propertyType">The data type of the database column. Defaults to <see cref="string"/> if not specified.</param>
6161
/// <param name="columnName">The name of the database column. Defaults to <paramref name="propertyName"/> if not specified.</param>
62+
/// <param name="size">The maximum size of the database column. Defaults to <c>null</c> if not specified.</param>
6263
/// <returns>The current <see cref="SqlServerSinkOptions"/> instance for method chaining.</returns>
6364
/// <remarks>
6465
/// This method provides a convenient way to add custom property mappings while configuring the sink.
@@ -67,13 +68,16 @@ public class SqlServerSinkOptions : BatchingOptions
6768
public SqlServerSinkOptions AddPropertyMapping(
6869
string propertyName,
6970
Type? propertyType = null,
70-
string? columnName = null)
71+
string? columnName = null,
72+
int? size = null)
7173
{
7274
var mapping = new ColumnMapping<LogEvent>
7375
(
7476
ColumnName: columnName ?? propertyName,
7577
ColumnType: propertyType ?? typeof(string),
76-
GetValue: logEvent => logEvent.GetPropertyValue(propertyName)
78+
GetValue: logEvent => logEvent.GetPropertyValue(propertyName),
79+
Nullable: true,
80+
Size: size
7781
);
7882

7983
Mappings.Add(mapping);

0 commit comments

Comments
 (0)