C
C#2y ago
gio735

✅ serilog and appsettings.json unable to build configuration

Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(
"../logs/webapi-Information-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
.WriteTo.File(
"../logs/webapi-Error-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error)
.CreateLogger();
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File(
"../logs/webapi-Information-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information)
.WriteTo.File(
"../logs/webapi-Error-.log",
rollingInterval: RollingInterval.Day,
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error)
.CreateLogger();
I am trying to write this in appsettings did it as such:
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore": "Warning",
"System": "Error"
}
},
"WriteTo:Information": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Information-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo:Error": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Error-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
"Serilog": {
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File"
],
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.AspNetCore": "Warning",
"System": "Error"
}
},
"WriteTo:Information": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Information-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo:Error": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Error-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
],
"WriteTo": [
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
}
It shows exception that WriteTo:Error:Name has no Name element
4 Replies
Anton
Anton2y ago
because it's an array you need WriteTo:Error[0]:Name or something
undisputed world champions
"WriteTo" should contain a array of all your sink you want to write to:
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Information-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
},
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Error-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
},
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Information-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
},
{
"Name": "File",
"Args": {
"path": "../logs/webapi-Error-.log",
"rollingInterval": "Day",
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
},
{
"Name": "Console",
"Args": {
"formatter": "Serilog.Formatting.Compact.RenderedCompactJsonFormatter, Serilog.Formatting.Compact"
}
}
]
you can add filters to specify which log-levels should go to which sink
Anton
Anton2y ago
Right, you're not reading it yourself
gio735
gio735OP2y ago
Thanks a lot
Want results from more Discord servers?
Add your server