Serilog ThreadId 始终输出为 1
Serilog ThreadId is always output as 1
我在 outputTemplate 中有一个 ThreadId,但日志始终将其记录为 1。我有多个应用程序实例 运行ning 同时进入同一个日志,因此希望我可以将基于 ThreadId 的实例。
谁能建议如何解决这个问题? ThreadName 在应用程序设置一些东西并确定它所在的区域后分配 运行,因此不是最好的分隔符。一个区域也可以同时有多个功能运行。因此需要 ThreadId
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.WithThreadName()
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ThreadId}> {Message:lj}{NewLine}{Exception}")
.WriteTo.File(@".\log\log.txt",
rollingInterval: RollingInterval.Day,
shared: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ThreadId}> {Message:lj}{NewLine}{Exception}")
.CreateLogger();
日志输出
2021-04-09 06:30:09.059 [INF] <blackstone> <1> Action : Task A
2021-04-09 06:30:09.059 [INF] <wavell> <1> Action : Task A
2021-04-09 06:30:09.060 [INF] <forest> <1> Action : Task A
2021-04-09 06:30:09.130 [INF] <wavell> <1> Loading CentreDetails
2021-04-09 06:30:09.130 [INF] <forest> <1> Loading CentreDetails
2021-04-09 06:30:09.132 [INF] <blackstone> <1> Loading CentreDetails
2021-04-09 06:30:09.560 [INF] <wavell> <1> Loading ParentDetails
2021-04-09 06:30:09.554 [INF] <blackstone> <1> Loading ParentDetails
2021-04-09 06:30:09.560 [INF] <forest> <1> Loading ParentDetails
听起来你想要 Serilog.Enrichers.Process
和 Enrich.WithProcessId()
而不是 WithThreadId()
。
好的,我遵循@Nicholas Blumhardt 的目标...
添加了 Serilog.Enrichers.Process nuget 并将代码更新为此
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.Enrich.WithProcessId()
.Enrich.WithThreadName()
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ProcessId}> {Message:lj}{NewLine}{Exception}")
.WriteTo.File(@".\log\log.txt",
rollingInterval: RollingInterval.Day,
shared: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ProcessId}> {Message:lj}{NewLine}{Exception}")
.CreateLogger();
日志现在推出了一个唯一的进程 ID
这对我有用:
nuget 包
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
appsettings.json {ThreadId}
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Error"
}
},
{
"Name": "File",
"Args": {
"path": "/log/api.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] Th:{ThreadId} {Message} {NewLine}{Exception}",
"rollingInterval": "Day"
}
}
],
"Properties": {
"Application": "acme"
}
}
}
启动.Enrich.WithThreadId()
public Startup(IConfiguration configuration)
{
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).Enrich.WithThreadId()
.CreateLogger();
Configuration = configuration;
}
示例日志
2022-03-11 14:49:25.425 -05:00 [Information] Th:27 foo
2022-03-11 14:49:25.428 -05:00 [Information] Th:27 Bar
我在 outputTemplate 中有一个 ThreadId,但日志始终将其记录为 1。我有多个应用程序实例 运行ning 同时进入同一个日志,因此希望我可以将基于 ThreadId 的实例。
谁能建议如何解决这个问题? ThreadName 在应用程序设置一些东西并确定它所在的区域后分配 运行,因此不是最好的分隔符。一个区域也可以同时有多个功能运行。因此需要 ThreadId
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.WithThreadName()
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ThreadId}> {Message:lj}{NewLine}{Exception}")
.WriteTo.File(@".\log\log.txt",
rollingInterval: RollingInterval.Day,
shared: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ThreadId}> {Message:lj}{NewLine}{Exception}")
.CreateLogger();
日志输出
2021-04-09 06:30:09.059 [INF] <blackstone> <1> Action : Task A
2021-04-09 06:30:09.059 [INF] <wavell> <1> Action : Task A
2021-04-09 06:30:09.060 [INF] <forest> <1> Action : Task A
2021-04-09 06:30:09.130 [INF] <wavell> <1> Loading CentreDetails
2021-04-09 06:30:09.130 [INF] <forest> <1> Loading CentreDetails
2021-04-09 06:30:09.132 [INF] <blackstone> <1> Loading CentreDetails
2021-04-09 06:30:09.560 [INF] <wavell> <1> Loading ParentDetails
2021-04-09 06:30:09.554 [INF] <blackstone> <1> Loading ParentDetails
2021-04-09 06:30:09.560 [INF] <forest> <1> Loading ParentDetails
听起来你想要 Serilog.Enrichers.Process
和 Enrich.WithProcessId()
而不是 WithThreadId()
。
好的,我遵循@Nicholas Blumhardt 的目标...
添加了 Serilog.Enrichers.Process nuget 并将代码更新为此
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(builder.Build())
.Enrich.FromLogContext()
.Enrich.WithProcessId()
.Enrich.WithThreadName()
.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ProcessId}> {Message:lj}{NewLine}{Exception}")
.WriteTo.File(@".\log\log.txt",
rollingInterval: RollingInterval.Day,
shared: true,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level:u3}] <{ThreadName}> <{ProcessId}> {Message:lj}{NewLine}{Exception}")
.CreateLogger();
日志现在推出了一个唯一的进程 ID
这对我有用:
nuget 包
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />
appsettings.json {ThreadId}
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Error"
}
},
{
"Name": "File",
"Args": {
"path": "/log/api.log",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] Th:{ThreadId} {Message} {NewLine}{Exception}",
"rollingInterval": "Day"
}
}
],
"Properties": {
"Application": "acme"
}
}
}
启动.Enrich.WithThreadId()
public Startup(IConfiguration configuration)
{
Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).Enrich.WithThreadId()
.CreateLogger();
Configuration = configuration;
}
示例日志
2022-03-11 14:49:25.425 -05:00 [Information] Th:27 foo
2022-03-11 14:49:25.428 -05:00 [Information] Th:27 Bar