Skip to content

Commit 858bf8d

Browse files
committed
debugging, and moved NonClusteredIndex property to CommonColumnOptions base class in prep for #81
1 parent 6a191d2 commit 858bf8d

File tree

5 files changed

+22
-23
lines changed

5 files changed

+22
-23
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ If you eliminate the Id column completely, the log table is stored as an unindex
164164

165165
### Unclustered Id Column
166166

167-
You can also retain the Id column as an `IDENTITY` primary key, but without a clustered index. The log is still stored as an unindexed heap, but writes with non-clustered indexes are slightly faster. The non-clustered indexes will reference the Id primary key. However, read performance will be slightly degraded since it requires two reads (the covering non-clustered index, then dereferencing the heap row from the Id). To create this type of table ahead of time, change the constraint in the previous section to `NONCLUSTERED` and leave out the `WITH` clause.
167+
You can also retain the Id column as an `IDENTITY` primary key, but using a non-clustered index. The log is still stored as an unindexed heap, but writes with non-clustered indexes are slightly faster. Non-clustered indexes on other columns will reference the Id primary key. However, read performance will be slightly degraded since it requires two reads (the covering non-clustered index, then dereferencing the heap row from the Id). To create this type of table ahead of time, change the constraint in the previous section to `NONCLUSTERED` and leave out the `WITH` clause.
168168

169169
### Bigint Data Type
170170

@@ -291,7 +291,7 @@ As the name suggests, `columnOptionSection` is an entire configuration section i
291291
{ "ColumnName": "Release", "DataType": "varchar", "DataLength": 32 }
292292
],
293293
"disableTriggers": true,
294-
"id": { "columnName": "Id", "bigint": true, "clusteredIndex": true },
294+
"id": { "columnName": "Id", "bigint": true, "nonClusteredIndex": true },
295295
"level": { "columnName": "Level", "storeAsEnum": false },
296296
"properties": {
297297
"columnName": "Properties",

src/Serilog.Sinks.MSSqlServer/Configuration/Microsoft.Extensions.Configuration/LoggerConfigurationMSSqlServerExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private static ColumnOptions ConfigureColumnOptions(ColumnOptions columnOptions,
221221
{
222222
SetIfProvided<string>((val) => { opts.Id.ColumnName = val; }, section["columnName"]);
223223
SetIfProvided<bool>((val) => { opts.Id.BigInt = val; }, section["bigInt"]);
224-
SetIfProvided<bool>((val) => { opts.Id.ClusteredIndex = val; }, section["clusteredIndex"]);
224+
SetIfProvided<bool>((val) => { opts.Id.NonClusteredIndex = val; }, section["nonClusteredIndex"]);
225225
}
226226

227227
section = config.GetSection("level");
@@ -264,11 +264,11 @@ private static ColumnOptions ConfigureColumnOptions(ColumnOptions columnOptions,
264264
SetIfProvided<bool>((val) => { opts.LogEvent.ExcludeAdditionalProperties = val; }, section["excludeAdditionalProperties"]);
265265
}
266266

267-
SetIfProvided<string>((val) => { opts.Id.ColumnName = val; }, config.GetSection("message")["columnName"]);
267+
SetIfProvided<string>((val) => { opts.Message.ColumnName = val; }, config.GetSection("message")["columnName"]);
268268

269-
SetIfProvided<string>((val) => { opts.Id.ColumnName = val; }, config.GetSection("exception")["columnName"]);
269+
SetIfProvided<string>((val) => { opts.Exception.ColumnName = val; }, config.GetSection("exception")["columnName"]);
270270

271-
SetIfProvided<string>((val) => { opts.Id.ColumnName = val; }, config.GetSection("messageTemplate")["columnName"]);
271+
SetIfProvided<string>((val) => { opts.MessageTemplate.ColumnName = val; }, config.GetSection("messageTemplate")["columnName"]);
272272

273273
return opts;
274274

src/Serilog.Sinks.MSSqlServer/Sinks/MSSqlServer/ColumnOptions.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public ColumnOptions()
1919
{
2020
Id = new IdColumnOptions
2121
{
22-
// Defaults for backwards compatibility only; not recommended, see docs
22+
// Defaults for backwards compatibility, see docs
2323
BigInt = false,
24-
ClusteredIndex = true
24+
NonClusteredIndex = false
2525
};
2626

2727
Level = new LevelColumnOptions();
@@ -128,12 +128,6 @@ public class IdColumnOptions : CommonColumnOptions
128128
/// If true, stores as bigint (max value 2^64-1) instead of the default int.
129129
/// </summary>
130130
public bool BigInt { get; set; }
131-
132-
/// <summary>
133-
/// If false, the log table will be stored as a heap structure. This will improve
134-
/// write performance at the cost of query performance. See documentation for details.
135-
/// </summary>
136-
public bool ClusteredIndex { get; set; }
137131
}
138132

139133
/// <summary>
@@ -241,6 +235,14 @@ public class CommonColumnOptions
241235
/// The name of the column in the database.
242236
/// </summary>
243237
public string ColumnName { get; set; }
238+
239+
/// <summary>
240+
/// Currently only implemented for the optional Id IDENTITY PK column. Defaults to false.
241+
/// If true, the log table will be stored as a heap structure. This can improve
242+
/// write performance at the cost of query performance. See documentation for details.
243+
/// </summary>
244+
public bool NonClusteredIndex { get; set; }
245+
244246
}
245247

246248
/// <summary>

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,12 @@ private DataTable CreateDataTable()
265265
{
266266
DataType = ColumnOptions.Id.BigInt ? typeof(long) : typeof(int),
267267
ColumnName = ColumnOptions.Id.ColumnName ?? StandardColumn.Id.ToString(),
268-
AutoIncrement = true
268+
AutoIncrement = true,
269+
AllowDBNull = false
269270
};
271+
id.ExtendedProperties.Add("NonClusteredIndex", ColumnOptions.Id.NonClusteredIndex);
270272
eventsTable.Columns.Add(id);
271-
id.ExtendedProperties.Add("clusteredIndex", false);
272-
if(ColumnOptions.Id.ClusteredIndex)
273-
{
274-
id.ExtendedProperties["clusteredIndex"] = true;
275-
eventsTable.PrimaryKey = new DataColumn[] { id };
276-
}
273+
eventsTable.PrimaryKey = new DataColumn[] { id };
277274
break;
278275

279276
case StandardColumn.Level:

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ private static string GetSqlFromDataTable(string tableName, DataTable table, str
6363
// primary keys
6464
if (hasPrimaryKey)
6565
{
66-
var clusteredIndex = (bool)table.PrimaryKey[0].ExtendedProperties["clusteredIndex"];
67-
sql.AppendFormat(" CONSTRAINT [PK_{0}] PRIMARY KEY {1}CLUSTERED (", tableName, clusteredIndex ? string.Empty : "NON" );
66+
var NonClusteredIndex = (bool)table.PrimaryKey[0].ExtendedProperties["NonClusteredIndex"];
67+
sql.AppendFormat(" CONSTRAINT [PK_{0}] PRIMARY KEY {1}CLUSTERED (", tableName, NonClusteredIndex ? "NON" : string.Empty);
6868

6969
int numOfKeys = table.PrimaryKey.Length;
7070
i = 1;

0 commit comments

Comments
 (0)