Problem switching configuration from code to appsettings.json
See original GitHub issueIn my project I’m trying to switch from the Fluent configuration in code to an appsettings.json
one.
Here there is my C# Fluent configuration
ColumnOptions columnOptions = new ColumnOptions();
columnOptions.Store.Remove(StandardColumn.Properties);
columnOptions.Store.Add(StandardColumn.LogEvent);
columnOptions.Level.StoreAsEnum = true;
LoggingLevelSwitch levelSwitch = new LoggingLevelSwitch();
levelSwitch.MinimumLevel = LogEventLevel.Debug;
Log.Logger = new LoggerConfiguration()
.MinimumLevel.ControlledBy(levelSwitch)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.WithMachineName()
.Enrich.WithProperty("Application", "My App")
.Enrich.WithProperty("Environment", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"))
.Enrich.WithThreadId()
.Enrich.With<EventTypeEnricher>()
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss.fff} {ThreadId} {EventType:x8} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.MSSqlServer(configuration.GetConnectionString("LogConnectionString"), "Logs", columnOptions: columnOptions, autoCreateSqlTable: true)
.CreateLogger();
And this is my actual appsettings.json
{
"ConnectionStrings": {
"DbContext": "Server=..."
},
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.MSSqlServer", "MyProject.AssemblyName" ],
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "EventTypeEnricher" ],
"Properties": {
"Application": "My App",
"Environment": "Development"
},
"LevelSwitches": { "$levelSwitch": "Debug" },
"WriteTo": [
{
"Name": "Console",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss.fff} {ThreadId} {EventType:x8} {Level:u3}] {Message:lj}{NewLine}{Exception}"
}
},
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=...",
"tableName": "Logs",
"columnOptionsSection": {
"addStandardColumns": [ "LogEvent" ],
"removeStandardColumns": [ "Properties" ],
"level": {
"columnName": "Level",
"storeAsEnum": true
}
},
"autoCreateSqlTable": true
}
}
]
}
}
Now I’m currently dealing with some problems:
-
My custom Enricher
EventTypeEnricher
, defined inside theMyProject.AssemblyName
that I added toUsing
, is not loaded. I can see because of the Console output template shows a blank space instead of the numeric values. -
Adding the MSSqlServer block leads to the following Exception:
Exception has been thrown by the target of an invocation. {System.Data.DuplicateNameException: A column named ‘Level’ already belongs to this DataTable.}
-
I’m not sure about configuration of
LevelSwitches
… -
Is it possible to configure the
Properties.Environment
value dynamically, like it is in my code?
The in code configuration is fully functional. My actual versions of Serilog packages are:
- Serilog.AspNetCore v2.1.1
- Serilog.Sinks.Console v3.1.1
- Serilog.Sinks.MSSqlServer v5.1.3-dev-00202
- Serilog.Settings.Configuration v3.0.0-dev-00119
- Serilog.Enrichers.Environment v2.1.2
Thanks!
UPDATE
About problem with MSSqlServer configuration, if I remove the
"level": {
"columnName": "Level",
"storeAsEnum": true
}
block, I’ve no more Exceptions, but that leads (obviously) to a runtime Exception:
Unable to write 2 log events to the database due to following error: The given value of type String from the data source cannot be converted to type tinyint of the specified target column.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
@MV10 And so, I’m set now! 👍 My JSON configuration is actually working.
About the
EventTypeEnricher
, I found that I was missing the appropriate extension method:and the correct configuration on JSON file:
Thank you very much!
Glad to help.