如何让Serilog丰富日志
How to get Serilog to enrich logs
我已将 Serilog 设置为从 appsettings.json
:
读取其配置
return WebHost.CreateDefaultBuilder(args)
.UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })
appsettings.json 有以下指定浓缩剂的相关信息:
{
"Serilog": {
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "%WIDGETSAPIBASEDIR%\logs\log-{Date}.txt"
}
},
{
"Name": "Debug"
}
],
"Enrich": [ "CorrelationId" ]
}
}
生成的日志不包含 Enrich
属性 中指定的任何数据。
我已经导入了 Serilog.Enrichers.CorrelationId
,但仍然没有任何结果。
我也试过 "Enrich": [ "WithCorrelationId" ]
。我也试过其他浓缩剂("FromLogContext", "WithMachineName", "WithThreadId")
,但还是一无所获。
我错过了什么?
这是我的有效配置文件示例。请注意我有丰富的输出模板 ?
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": ".//Logs//app.log-.txt",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"outputTemplate": "[{Timestamp :HH:mm:ss} {Level:u3} {SourceContext, -20} {ProcessId} {ProcessName} {ThreadId}] {Message}\n{Exception}",
,
"shared": true
}
},
{
"Name": "Console"
}
]
}
},
{
"Name": "SpectreConsole",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} [{Level:u3} {ProcessId}] {Message:lj}{NewLine}{Exception}",
"minLevel": "Verbose"
}
}
],
"Enrich": [
"FromLogContext",
"WithMemoryUsage",
"WithProcessId",
"WithProcessName",
"WithThreadId",
"WithThreadName"
]
}
我已将 Serilog 设置为从 appsettings.json
:
return WebHost.CreateDefaultBuilder(args)
.UseSerilog((ctx, config) => { config.ReadFrom.Configuration(ctx.Configuration); })
appsettings.json 有以下指定浓缩剂的相关信息:
{
"Serilog": {
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "%WIDGETSAPIBASEDIR%\logs\log-{Date}.txt"
}
},
{
"Name": "Debug"
}
],
"Enrich": [ "CorrelationId" ]
}
}
生成的日志不包含 Enrich
属性 中指定的任何数据。
我已经导入了 Serilog.Enrichers.CorrelationId
,但仍然没有任何结果。
我也试过 "Enrich": [ "WithCorrelationId" ]
。我也试过其他浓缩剂("FromLogContext", "WithMachineName", "WithThreadId")
,但还是一无所获。
我错过了什么?
这是我的有效配置文件示例。请注意我有丰富的输出模板 ?
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "File",
"Args": {
"path": ".//Logs//app.log-.txt",
"rollingInterval": "Day",
"rollOnFileSizeLimit": true,
"outputTemplate": "[{Timestamp :HH:mm:ss} {Level:u3} {SourceContext, -20} {ProcessId} {ProcessName} {ThreadId}] {Message}\n{Exception}",
,
"shared": true
}
},
{
"Name": "Console"
}
]
}
},
{
"Name": "SpectreConsole",
"Args": {
"outputTemplate": "[{Timestamp:HH:mm:ss} [{Level:u3} {ProcessId}] {Message:lj}{NewLine}{Exception}",
"minLevel": "Verbose"
}
}
],
"Enrich": [
"FromLogContext",
"WithMemoryUsage",
"WithProcessId",
"WithProcessName",
"WithThreadId",
"WithThreadName"
]
}