I've been trying to configure Serilog to use the MSSqlServer
sink. As long as I'm using the default error table provided by Serilog, then it works as expected. However, I'm going to be using the same table in the same database for multiple projects, so I wanted to add a "Source" column which would contain the current assembly's name automatically.
I have the following:
"Serilog": {
"Using": [
"Serilog.Sinks.File",
"Serilog.Sinks.MSSqlServer"
],
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/log-.txt",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"retainedFileCountLimit": 7,
"fileSizeLimitBytes": 10000000,
"flushToDiskInterval": "00:00:10"
}
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=xxxxxxxx",
"tableName": "Error_Logs",
"autoCreateSqlTable": true,
"columnOptions": {
"additionalColumns": [
{
"ColumnName": "Source",
"DataType": "nvarchar(128)",
"AllowNull": false
}
],
"customColumns": [
{
"PropertyName": "Source",
"ColumnName": "Source",
"DataType": "nvarchar(128)"
}
],
"minimumLevel": "Error"
}
}
}
]
}
In my Program.cs, I have the following:
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Configuration, "AppSettings:Serilog")
.Enrich.FromLogContext()
.CreateLogger();
The issue is that I am getting this:
2023-02-20T15:40:48.8728390Z Unable to write batch of 1 log events to the database due to following error: Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'Source', table 'XXXX.dbo.Error_Logs'; column does not allow nulls. INSERT fails.
The statement has been terminated.
at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at Microsoft.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at Microsoft.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at Microsoft.Data.SqlClient.SqlBulkCopy.RunParser(BulkCopySimpleResultSet bulkCopyHandler)
at Microsoft.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinuedOnSuccess(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at Microsoft.Data.SqlClient.SqlBulkCopy.CopyBatchesAsyncContinued(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at Microsoft.Data.SqlClient.SqlBulkCopy.CopyBatchesAsync(BulkCopySimpleResultSet internalResults, String updateBulkCommandText, CancellationToken cts, TaskCompletionSource`1 source)
at Microsoft.Data.SqlClient.SqlBulkCopy.WriteToServerInternalRestContinuedAsync(BulkCopySimpleResultSet internalResults, CancellationToken cts, TaskCompletionSource`1 source)
--- End of stack trace from previous location ---
I have tried a bunch of things such as:
Adding: .Enrich.WithProperty("Source", Assembly.GetExecutingAssembly().GetName().Name)
Adding: LogContext.PushProperty("Source", Assembly.GetExecutingAssembly().GetName().Name);
Adding: "Enrich": ["WithAssemblyName"]
at the end of the appsettings.json file
Etc... But nothing has worked and all have produced this issue.