I am trying to implement Serilog sink for SQL Server (v. 13.0) in a .NET Core 1.1 Console App, but when I try to write in the table I define, the result is unfortunately always an empty.
I tried to follow the documentation provided in GitHub and other suggestions found in Stack Overflow but nothing could help me properly. I am probably missing something, but what?
The code I'm trying to implement in the Main is as follows
private static Serilog.ILogger _seriLogger;
. . . . . . .
var columnOptions = new ColumnOptions();
columnOptions.Store.Remove(StandardColumn.MessageTemplate);
// Rename columns
columnOptions.Exception.ColumnName = "Controller";
columnOptions.Properties.ColumnName = "Action";
// Add a custom column
columnOptions.AdditionalDataColumns = new Collection<DataColumn>
{
new DataColumn { AllowDBNull = true, ColumnName = "ExtraInfo", DataType = typeof(string)}
};
var serviceProvider = new ServiceCollection()
.AddLogging()
.AddSingleton<Serilog.ILogger>(x =>
{
return new LoggerConfiguration()
.WriteTo.MSSqlServer("Server=(localdb)\\HR2014;Database=MyLog;User Id=serilogdb;Password=********;", "Logs",
autoCreateSqlTable: true,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Debug,
batchPostingLimit: 1,
period: TimeSpan.Zero
/* , columnOptions: columnOptions */)
.CreateLogger();
})
.BuildServiceProvider();
//configure logging
serviceProvider
.GetService<ILoggerFactory>()
.AddSerilog();
_seriLogger = serviceProvider.GetService<Serilog.ILogger>();
_seriLogger.Debug("test");
I wish to point out a couple of things:
- I don't get any compiler or run-time error;
- The table Logs is successfully created but when I uncomment the columnOptions the table is not created at all and no error message is shown.
When I try
_seriLogger.Information("test");
rather than
_seriLogger.Debug("test");
I have the error message: System.StackOverflowException.