【发布时间】:2020-10-11 20:24:47
【问题描述】:
我在我的 dotnet 核心应用程序中使用 Serilog。我已将自定义列添加到 Serilog 提供的默认列列表中。下面是我的“Serilog”配置在 appsettings.json 文件中的样子 -
"Serilog": {
"MinimumLevel": "Information",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": <connectionString>
"tableName": "Log",
"autoCreateSqlTable": true,
"columnOptionsSection": {
"removeStandardColumns": [ "MessageTemplate", "Properties"], //remove the Properties column in the standard ones
"customColumns": [
{
"ColumnName": "ControllerName",
"DataType": "varchar",
"DataLength": 50
}
]
},
"timeStamp": {
"columnName": "Timestamp",
"convertToUtc": true
}
}
}
]
}
所以我从 Serilog 创建的默认列列表中删除了“MessageTemplate”和“Properties”,并将 “ControllerName” 作为新列添加到表 Log ,其中 Serilog 记录其数据。我想要的是,当我记录信息时,我想为“ControllerName”列提供值。如何做呢? 我找到了以下解决方案:
_logger.LogInformation("{ControllerName}{Message}", "TestController", "Starting up..");
这行代码为 ControllerName 列提供值,但 message 列获取值
"TestController""Starting up.."
我希望消息列获得值
Starting up..
【问题讨论】:
-
@CamiloTerevinto :“ControllerName”只是一个示例,也是 POC 的一部分。实际上,我想使用一个或多个自定义列并在记录时为它们提供值。列,如 - 服务名称等。
-
您可以使用
IDiagnosticContext接口设置附加属性。 Logging the selected Endpoint Name with Serilog这里可以作为起点 -
@PavelAnikhouski - 我发现这很有用 - github.com/serilog/serilog/wiki/Enrichment 我想知道使用 LogContext.PushProperty() 可以吗?任何性能问题?
标签: c# asp.net-core serilog