asp.net-core - 如何在 asp.net 核心日志记录和应用程序洞察中记录 json
问题描述
有没有办法在 asp.net core 2 中记录以下内容?
_logger.LogInformation("Someone did something! \r\n{detail}", new {
SomeObject = someVariable,
SomeOtherObject = someOtherVariable
});
我已经尝试使用序列化值,但生成的 json 似乎被截断了。
解决方案
您尝试使用的参数用于 args,通常用于消息的字符串插值。一些日志记录提供程序,例如 Serilog,会将这些 args 保存为 JSON,但是您依赖于特定的提供程序以及他们选择处理它的方式。
如果您的目标是简单地{detail}
用 JSON 对象替换,那么直接在消息中执行此操作:
var detail = JsonConvert.SerializeObject(new
{
SomeObject = someVariable,
SomeOtherObject = someOtherVariable
});
_logger.LogInformation($"Someone did something! \r\n{detail}");
推荐阅读
- javascript - 等价于 image/png;base64 用于 excel 文件流
- reactjs - 如何使用 Spring MVC 和 React 作为前端服务器?
- visual-c++ - VS 编译器是否提供了一个宏变量来判断浮点模型是否精确?
- sql - 使用循环的sql过程打印错误结果
- dialogflow-es - agent.add(conv) 在 dialogflow-fulfillment-library 中不起作用?
- swift - 为什么这个函数没有返回任何东西?
- php - 如何在同一页面中使用 html 和 php 在下拉的选择更改事件的输入字段中从数据库中获取选定的数据
- arrays - 使用 mongodb 的 java 驱动程序从集合中获取数组字段
- c# - .Net Core 2 OpenID Connect 身份验证和多个身份
- google-cloud-platform - Apache Beam python sdk 因 IllegalArgumentException 而失败