serilog - 如何在 Serilog 中记录消息模板的字段名称
问题描述
Serilog 中是否有任何设置告诉在使用输出模板时记录消息模板中的变量名称?
_logger.LogWarning(@"Role with Id {RoleId} not found", id);
我使用日志记录到文件并希望看到文件中记录的 RoleId 字段以及消息,因此我可以使用此字段进行过滤。我的 Serilog 设置看起来像
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft.EntityFrameworkCore.Storage.IRelationalCommandBuilderFactory": "Warning"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "bin/Debug/netcoreapp2.0/Logs/log-.txt",
"rollingInterval": "Day",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} {Level:u3} {SourceContext} {Message:lj} {RequestId} {Properties} {NewLine} {Exception}"
//"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
}
}]
}
我知道我可以使用LogContext.PushProperty("RoleId", id)
,但在我的情况下这不是一个选项以及使用CompactJsonFormatter
.
解决方案
我发现如果日志被写入文件并且$
后面跟着字段(不是对象)(_logger.LogWarning("Role with Id {$RoleId} not found", id);)
Serilog 将生成消息模板,带有 $sign 的字段名称和字段值。{Properties}
也需要在outputTemplate
.
推荐阅读
- azure-functions - 在没有 SecretVersion 的情况下访问 Azure Key Vault 机密
- python - 如何在 tkinter 窗口打开时运行 while 循环?
- r - 解决 R 中函数名称冲突的最佳方法是什么?
- php - 为什么这个 jQuery 函数在某些浏览器上部分工作?
- xgboost - 使用 XgBoostClassifier 预测 Xgboost DMatrix 对象
- keras - 在 Keras 中使用 LSTM 获取单词用于文本分类的概率
- python - 如何每 3 小时迭代一次 for 循环,总共 7 天
- java - Spring boot rest在注册时设置用户默认配置文件图像
- date - 这种表示日期的二进制格式是什么?
- performance - 气流 - 装满 DagBag 真的很慢