我有一个带有 Target Framework 8.0 的控制台应用程序。
我想将 ThreadId 记录到我的 Serilog 日志的控制台输出。 ThreadId 已记录到 json 文件,但在控制台中它不记录 ThreadId。 以下是我的配置,
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Async" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "logs/schedular_log_.json",
"rollingInterval": "Day",
"shared": true,
"rollOnFileSizeLimit": true,
"retainedFileCountLimit": 7,
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
]
}
}
],
"Enrich": [ "WithThreadId", "FromLogContext", "WithExceptionDetails" ],
"Properties": {
"Application": "Schedular"
}
}
}
以下是控制台日志中的结果,
[13:29:12 INF] {"Id": "bb497c8e-9e92-4981-9c98-d3e94ed1b009", "Name": "Mat0", "Created": "2024-04-01T13:29:12.0955808+05:30", "Description": "MatDescription0", "$type": "Material"} <s:>
[13:29:12 INF] {"Id": "98ccbe47-e62a-406a-9eb2-01c7314131e9", "Name": "Mat1", "Created": "2024-04-01T13:29:12.2064649+05:30", "Description": "MatDescription1", "$type": "Material"} <s:>
但是在我的 json 文件日志中,我得到以下结果,
{"Timestamp":"2024-04-01T13:29:12.2068098+05:30","Level":"Information","MessageTemplate":"{@Material}","Properties":{"Material":{"Id":"aac4815f-5ea1-4527-9558-6f0d868cd41c","Name":"Mat7","Created":"2024-04-01T13:29:12.2068090+05:30","Description":"MatDescription7","_typeTag":"Material"},"ThreadId":1,"Application":"Schedular"}}
{"Timestamp":"2024-04-01T13:29:12.2068150+05:30","Level":"Information","MessageTemplate":"{@Material}","Properties":{"Material":{"Id":"25944fb5-a50a-4b74-b561-79c775fadd2b","Name":"Mat8","Created":"2024-04-01T13:29:12.2068147+05:30","Description":"MatDescription8","_typeTag":"Material"},"ThreadId":1,"Application":"Schedular"}}
{"Timestamp":"2024-04-01T13:29:12.2068182+05:30","Level":"Information","MessageTemplate":"{@Material}","Properties":{"Material":{"Id":"6d59c4a6-ba2d-46c8-b3ce-f7ccd7dfff9c","Name":"Mat9","Created":"2024-04-01T13:29:12.2068181+05:30","Description":"MatDescription9","_typeTag":"Material"},"ThreadId":1,"Application":"Schedular"}}
注意文件日志中的 ThreadId,而不是控制台日志中的 ThreadId。有没有办法让控制台记录ThreadId。 我更愿意仅通过 json 配置。
您需要添加 Serilog.Enrichers.Thread 和 Serilog.Enrichers.Process nuget 包。更改您的 appsettings.json 文件,如下所示。我已将 “Serilog.Enrichers.Thread”、“Serilog.Enrichers.Process”、 添加到“使用”参数值中。
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.Thread","Serilog.Sinks.Async" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "Async",
"Args": {
"configure": [
{
"Name": "Console",
"Args": {
"theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "logs/schedular_log_.json",
"rollingInterval": "Day",
"shared": true,
"rollOnFileSizeLimit": true,
"retainedFileCountLimit": 7,
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
}
]
}
}
],
"Enrich": [ "WithThreadId", "FromLogContext", "WithExceptionDetails" ],
"Properties": {
"Application": "Schedular"
}
}
}
您需要将“.Enrich.WithThreadId()”添加到您的serilog记录器配置中。
var appLogger = new LoggerConfiguration()
.ReadFrom.Configuration(configFile)
.Enrich.WithThreadId()
.Enrich.WithProcessId()
//.Enrich.WithThreadName()
.Enrich.FromLogContext()
.CreateLogger();