4
I am using the Serilog log log library on ASP.NET Core 2.1
I am using based on the Serilog manual: https://github.com/serilog/serilog-sinks-mssqlserver
Normally the configuration file is in appsettings.json, but I need to get Connectionstring from another location. So I tried to merge between appsettings.json and settings, but unfortunately it didn’t work (then it doesn’t even create the table) here’s how it is: (OBS: This config, is described in the link above)
var logDB = "Aqui minha connection string";
var logTable = "LogError";
var options = new ColumnOptions();
options.Store.Add(StandardColumn.LogEvent);
options.LogEvent.DataLength = 2048;
options.PrimaryKey = options.TimeStamp;
options.TimeStamp.NonClusteredIndex = true;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Warning()
.WriteTo.MSSqlServer(
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information,
connectionString: logDB,
tableName: logTable,
columnOptions: options)
.Enrich.WithProperty("SetorErro", Common.SetorLogErro.MVC)
.CreateLogger();
In the above code I have also tried to put beyond the parameters via C#, the config by appsettings.json
, appConfiguration: Configuration)
In my appsettings file, I have tried to remove to not be duplicated as leave also the config, even setting above, and it does not work, only works via appsettings.json
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Data Source=xxx.yyy.zzz",
"tableName": "LogError",
"autoCreateSqlTable": true,
"restrictedToMinimumLevel": "Information ", //https://github.com/serilog/serilog/wiki/Writing-Log-Events
"columnOptionsSection": {
"customColumns": [
{
"ColumnName": "SetorErro",
"DataType": "int",
"AllowNull": true
},
{
"ColumnName": "LocalErro",
"DataType": "nvarchar",
"DataLength": 150,
"AllowNull": true
}
]
}
}
}
]
}
The only way it works is when I do . Readfrom
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
Summary:
I need to take part of the settings via appsettings.json and parde via hard code. I’ve opened an Issue in the project https://github.com/serilog/serilog-sinks-mssqlserver/issues/185
Do you need to bring connectionString via code? or do you want to use two connectionStrings within appSettings.json? was unclear in the question
– Julio Borges
thanks for warning, I edited to be clearer, I need to get the string Connection and some parameters via C# code and other config via appsettings.json, but I found that Asp.net core does not work almost anywhere via code.
– Dorathoto
yes, probably the best approach is to use the same appsettings, what happens is that if vc merge hard code with appsettings the library of the pyre serilog
– Julio Borges
@Julioborges but in serilog help they warn that can, and another detail, via hardcode does not work nda... seems to be a bug from the library to Asp.net core
– Dorathoto
yes, true. You tried to remove the appSettings settings and put only the hard coded configuration?
– Julio Borges
@Julioborges yes, but it doesn’t only work hardcode to open an Issue
– Dorathoto