为什么我从NLog FileTarget获取Kibana中的部分日志消息?
我使用NLog FileTarget将消息记录到一个文件中,该文件由filebeat拾取并发送到Kibana 我的消息通常是多行的 我注意到一些消息在Kibana中显示为剪辑,在多行消息中只有一些行 例如,在日志中,我有这样的东西为什么我从NLog FileTarget获取Kibana中的部分日志消息?,kibana,nlog,filebeat,Kibana,Nlog,Filebeat,我使用NLog FileTarget将消息记录到一个文件中,该文件由filebeat拾取并发送到Kibana 我的消息通常是多行的 我注意到一些消息在Kibana中显示为剪辑,在多行消息中只有一些行 例如,在日志中,我有这样的东西 2020-05-04 16:23:16.1523|::1|80000037-0000-fb00-b63f-84710c7967bb 2020-05-04 16:23:16.1523|::1|80000037-0000-fb00-b63f-84710c7967b
2020-05-04 16:23:16.1523|::1|80000037-0000-fb00-b63f-84710c7967bb
2020-05-04 16:23:16.1523|::1|80000037-0000-fb00-b63f-84710c7967bb||INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44328/Foo
2020-05-04 16:23:16.2287|::1|80000037-0000-fb00-b63f-84710c7967bb|user1|DEBUG|MyApp.Web.Controllers.FooController|Validation OK
2020-05-04 16:23:16.2530|::1|80000037-0000-fb00-b63f-84710c7967bb|user1|DEBUG|MyApp.Web.BusinessLogic.FooLogic|Query results time:3ms
2020-05-04 16:23:16.2687|::1|80000037-0000-fb00-b63f-84710c7967bb|user1|DEBUG|MyApp.Web.BusinessLogic.FooLogic|Count:0 time:1ms
2020-05-04 16:23:16.6127|::1|80000037-0000-fb00-b63f-84710c7967bb|user1|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished in 459.4438ms 200 text/html; charset=utf-8
但在基巴纳我只看到
2020-05-04 16:23:16.1523|::1|80000037-0000-fb00-b63f-84710c7967bb
2020-05-04 16:23:16.1523|::1|80000037-0000-fb00-b63f-84710c7967bb||INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2.0 GET https://localhost:44328/Foo
2020-05-04 16:23:16.2287|::1|80000037-0000-fb00-b63f-84710c7967bb|user1|DEBUG|MyApp.Web.Controllers.FooController|Validation OK
2020-05-04 16:23:16.2530|::1|80000037-0000-fb00-b63f-84710c7967bb|user1|DEBUG|MyApp.Web.BusinessLogic.FooLogic|Query results time:3ms
请注意,这只会发生在某些消息上,而不是所有消息,并且日志消息的大小似乎并不重要。我已经有相当长的消息被完整地记录下来,而较小的消息被截短
我能想到的唯一原因是,这条信息不是一次性写的,而是部分写的,kibana拾取了部分信息,忽略了其余部分。是这样吗?如果是,我可以将目标配置为一次写入所有消息吗
我的堆栈是:
- .NET核心3.1
- #
- NLog 4.7.0
- NLog.Web.AspNetCore 4.9.2
"target": {
"type": "File",
"fileName": "c:\\wwwlogs\\MyApp.Web\\Combined.log",
"archiveFileName": "c:\\wwwlogs\\MyApp.Web\\archives\\Combined.{#}.log",
"archiveEvery": "Day",
"archiveNumbering": "Rolling",
"maxArchiveFiles": "7",
"layout": "${longdate}|${aspnet-request-ip}|${aspnet-TraceIdentifier}|${aspnet-user-identity}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
}
FileBeat配置为:
- type: log
enabled: true
paths:
- C:\wwwlogs\MyApp.Web\Combined.log
multiline.pattern: '^[[:space:]]'
multiline.negate: false
multiline.match: after
令人尴尬,但事实证明这不是问题。整个消息由filebeat拾取,但仅部分显示在Kibana索引视图上。如果单击消息详细信息,则整个消息可见
因此,NLog和filebeat没有问题,这是Kibana中的一个视图问题,但事实证明这不是问题。整个消息由filebeat拾取,但仅部分显示在Kibana索引视图上。如果单击消息详细信息,则整个消息可见
因此,NLog和filebeat没有问题,这是Kibana中的一个视图问题不是filebeat方面的专家,但在阅读文档之后: 然后看起来您可以这样做:
- type: log
enabled: true
paths:
- C:\wwwlogs\MyApp.Web\Combined.log
input_type: log
json.message_key: msg
json.keys_under_root: false
json.add_error_key: true
json.overwrite_keys: false
然后可以使用NLog JsonLayout执行此操作:
"target": {
"type": "File",
"fileName": "c:\\wwwlogs\\MyApp.Web\\Combined.log",
"archiveFileName": "c:\\wwwlogs\\MyApp.Web\\archives\\Combined.{#}.log",
"archiveEvery": "Day",
"archiveNumbering": "Rolling",
"maxArchiveFiles": "7",
"layout": {
"type": "JsonLayout",
"Attributes": [
{
"name": "time",
"layout": "${date:format=o}"
},
{
"name": "lvl",
"layout": "${level}"
},
{
"name": "logger",
"layout": "${logger}"
},
{
"name": "msg",
"layout": "${message}"
},
{
"name": "req_traceid",
"layout": "${aspnet-TraceIdentifier}"
},
{
"name": "req_user",
"layout": "${aspnet-user-identity}"
},
{
"name": "req_ip",
"layout": "${aspnet-request-ip}"
},
{
"name": "error_type",
"layout": "${exception:format=type}"
},
{
"name": "exception",
"layout": "${exception:format=tostring}"
},
{
"name": "properties",
"encode": false,
"layout": {
"type": "JsonLayout",
"includeallproperties": "true"
}
}]
}
}
不是FileBeat方面的专家,但在阅读文档后: 然后看起来您可以这样做:
- type: log
enabled: true
paths:
- C:\wwwlogs\MyApp.Web\Combined.log
input_type: log
json.message_key: msg
json.keys_under_root: false
json.add_error_key: true
json.overwrite_keys: false
然后可以使用NLog JsonLayout执行此操作:
"target": {
"type": "File",
"fileName": "c:\\wwwlogs\\MyApp.Web\\Combined.log",
"archiveFileName": "c:\\wwwlogs\\MyApp.Web\\archives\\Combined.{#}.log",
"archiveEvery": "Day",
"archiveNumbering": "Rolling",
"maxArchiveFiles": "7",
"layout": {
"type": "JsonLayout",
"Attributes": [
{
"name": "time",
"layout": "${date:format=o}"
},
{
"name": "lvl",
"layout": "${level}"
},
{
"name": "logger",
"layout": "${logger}"
},
{
"name": "msg",
"layout": "${message}"
},
{
"name": "req_traceid",
"layout": "${aspnet-TraceIdentifier}"
},
{
"name": "req_user",
"layout": "${aspnet-user-identity}"
},
{
"name": "req_ip",
"layout": "${aspnet-request-ip}"
},
{
"name": "error_type",
"layout": "${exception:format=type}"
},
{
"name": "exception",
"layout": "${exception:format=tostring}"
},
{
"name": "properties",
"encode": false,
"layout": {
"type": "JsonLayout",
"includeallproperties": "true"
}
}]
}
}
为什么不使用NLog JsonLayout以Json格式输出:好吧,这并不能真正回答问题,不是吗?无论如何,关于“为什么不”,一个原因是我的自定义目标是为了使用简单的布局而构建的,所以还有更多的工作要做。如果我对问题的存在有一个明确的答案,并且没有办法用简单的布局来解决它,我会考虑它。但是,最好不要在那之前。@RolfKristensen如果我继续上面的内容,我的filebeat配置应该是什么样的,所以我最终在kibana中显示了与现在相同的消息?可能对所有控制真正json格式的kibana神犯了罪。但也许它可以给你一个开始(下面添加了答案)为什么不使用NLog JsonLayout以Json格式输出:好吧,这并不能真正回答这个问题,不是吗?无论如何,关于“为什么不”,一个原因是我的自定义目标是为了使用简单的布局而构建的,所以还有更多的工作要做。如果我对问题的存在有一个明确的答案,并且没有办法用简单的布局来解决它,我会考虑它。但是,最好不要在那之前。@RolfKristensen如果我继续上面的内容,我的filebeat配置应该是什么样的,所以我最终在kibana中显示了与现在相同的消息?可能对所有控制真正json格式的kibana神犯了罪。但也许它可以给你一个开始(下面补充答案),我可能会这样做,但在基巴纳帮助提取。thnx!我可能会这么做,但我会帮助基巴纳的提取。thnx!