[英]Serilog doesn't produce a valid json (structured logging)
我正在开发一个使用 asp.net core 和 Serilog 进行日志记录的应用程序。
我有以下代码:
Log.Information("Application Starting.");
Log.Information("Message: fetching all Requests");
我的 Serilog 配置文件如下所示:
{
"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"
}
}
}
}
]
}
问题:作为输出,我得到一个无效的 JSON 文件:
{"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"}
我希望得到这样的东西:
[
{
"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"
}
]
有人可以帮我解决这个问题吗!
您对 JSON 文件不是有效的 JSON 对象的说法是正确的。 在日志库的上下文中,输出不应被视为文件,而是数据流。 因此,每个日志条目都是可能永无止境的流中的一项。
这也是为什么在每行后添加一个开放数组指示符并添加逗号是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.