I followed the format for appsettings.json provided by the documentation and have the following:
"Serilog": {
"Using": [ "Serilog.Sinks.MSSqlServer", "Serilog.Sinks.File" ],
"MinimumLevel": "Verbose",
"Enrich": [ "FromLogContext", "WithThreadId" ],
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Default",
"schemaName": "log",
"tableName": "LogTable",
"autoCreateSqlTable": true,
"restrictedToMinimumLevel": "Verbose",
"batchPostingLimit": 100,
"period": "0.00:00:30",
"columnOptionsSection": {
"disableTriggers": true,
"clusteredColumnstoreIndex": false,
"primaryKeyColumnName": "Id",
"addStandardColumns": [ "LogEvent" ],
"removeStandardColumns": [ "Properties" ],
"additionalColumns": [
{
"ColumnName": "Username",
"DataType": "nvarchar",
"DataLength": 250,
"AllowNull": false
}
],
"id": {
"nonClusteredIndex": true,
"DataType": "bigint"
},
"level": {
"columnName": "Level",
"storeAsEnum": false
},
"timeStamp": {
"columnName": "CreatedDate",
"convertToUtc": false
},
"message": { "columnName": "Message" },
"exception": { "columnName": "Exception" },
"messageTemplate": { "columnName": "Template" }
}
}
},
{
"Name": "File",
"Args": {
"path": "C:\\Logs\\WishesLog.txt",
"rollOnFileSizeLimit": true,
"fileSizeLimitBytes": "5242880",
"outputTemplate": "{Timestamp:o} [{Level:u3}] ({Username}-{ThreadId}): {Message}{NewLine}{Exception}"
}
}
]
}
I also have the following Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog((context, config) => { config.ReadFrom.Configuration(context.Configuration); })
.UseStartup<Startup>();
}
The File Sink works correctly but unfortunately I have been unable to get my log entries in SQL Server. The table does not get created. I also do not get any exceptions throw regarding the DB Sink.
Discovered the issue was with the version of Serilog.Sinks.MsSqlServer. Using the latest dev version uses the json configuration correctly. The current stable version 5.12 does not work having the full configuration in json files but does work when configuring the logger in code.
Hope this helps.