Reputation: 47
I'm developing an application using asp.net core and Serilog for logging.
I have the following code:
Log.Information("Application Starting.");
Log.Information("Message: fetching all Requests");
My Serilog config file look like this:
{
"Serilog": {
"Using": [ "Serilog.Settings.Configuration" ],
"MinimumLevel": {
"Default": "Information",
"Override": {
"System": "Warning",
"Microsoft": "Warning"
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "..\\Logs\structuredLog.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"fileSizeLimitBytes": 20485760
}
}
],
"Enrich": [
"FromLogContext",
"WithMachineName",
"WithProcessId",
"WithThreadId"
],
"Properties": {
"ApplicationName": "Serilog.WebApplication"
}
}
}
}
]
}
The Problem: As an output i get an invalid JSON file:
{"Timestamp":"2021-07-27T10:09:41.9531148+02:00","Level":"Information","MessageTemplate":"Application Starting."}
{"Timestamp":"2021-07-27T10:09:46.7538684+02:00","Level":"Information","MessageTemplate":"Message: fetching all Requests"}
I expect to get something like this:
[
{
"Timestamp":"2021-07-27T10:09:41.9531148+02:00",
"Level":"Information",
"MessageTemplate":"Application Starting."
},
{
"Timestamp":"2021-07-27T10:09:46.7538684+02:00",
"Level":"Information",
"MessageTemplate":"Message: fetching all Requests"
}
]
Can someone please help me to solve this problem!
Upvotes: 3
Views: 1956
Reputation: 2922
After I generate the json log, I can adjust the format by using the format that comes with vs.
Format shortcut: Ctrl+K
or Ctrl+D
.
If it doesn't work, do it like this:
Result:
Upvotes: 0
Reputation: 10347
You are correct with the statement that the JSON file is not a valid JSON object. In the context of a logging library the output should not be seen as a file but a stream of data. So each log entry is one item in a possibly never ending flow.
This is also why it makes no sense to add an opening array indicator and adding comma after each line.
Upvotes: 2