c# - NLog 结构化日志记录将所有值转换为字符串
问题描述
我们正在使用 asp.net 核心的 NLog 集成来管理我们的日志。我们需要生成一个包含响应时间(持续时间)的 JSON 日志。
下面的代码:
logger.LogInformation("Duration {duration}", 122);
使用以下配置:
<target name="aws" type="Debugger">
<layout type="JsonLayout">
<attribute name="level" layout="${level}" />
<attribute name="msg" layout="${message}" />
<attribute name="err" layout="${exception:format=tostring}" />
<attribute name="meta" encode="false">
<layout type="JsonLayout">
<attribute name="requestId" layout="${aspnet-traceidentifier}" />
<attribute name="user" layout="${aspnet-user-identity}" />
<attribute name="agent" layout="${aspnet-request-useragent}" />
<attribute name="method" layout="${aspnet-request-method}" />
<attribute name="url" layout="${aspnet-request-url:IncludeHost=true:IncludePort=true:IncludeQueryString=true}" />
<attribute name="logger" layout="${logger}" />
<attribute name="duration" layout="${event-properties:duration}" />
</layout>
</attribute>
</layout>
</target>
始终生成如下所示的输出:
{
"level": "Info",
"msg": "Duration 122",
"meta": {
"requestId": "0HLKTC2E2B3KL:00000002",
"agent": "PostmanRuntime\/7.6.0",
"method": "POST",
"url": "http:\/\/localhost:20000\/api\/v1\/oauth\/token",
"logger": "TEC.CoreApi.Application.Features.Authentication.OAuthController",
"duration": "122"
}
}
如您所见,持续时间变成了一个字符串,这使我们无法使用我们的日志解析器 (CloudWatch Logs)。我绝对需要将持续时间作为数值。
关于如何解决这个问题的任何想法?
谢谢塞巴斯蒂安
解决方案
尝试更换
<attribute name="duration" layout="${event-properties:duration}"/>
和
<attribute name="duration" layout="${event-properties:duration}" encode="false"/>
推荐阅读
- java - java中的JDBC接口对象
- jquery - PHP jQuery:Append() 到选项卡正在复制内容,直到重新访问选项卡
- ruby - 无法将 webdriver 与 chromedriver-helper 或 webdrivers gem 一起使用
- oracle - 如何使用 :NEW 或 :OLD 运算符获取 oracle 触发器中的所有新值。像新的一样。*)
- mysql - mysql 在单个命令中查询以获取每个名称的总和
- reactjs - 为什么 href 在应用程序中的服务器上不起作用?
- sql - 在其中使用 when 语句和自定义代码
- javascript - Dropzone 没有定义,symfony 上有 webpack
- javascript - 隐藏嵌套 div 时悬停命令停止工作
- azure-functions - 定时器触发 Azure 函数在第二天没有被触发