KAH*_*tle 5 c# logging serilog-sinks-file
我在 outputTemplate 中有一个 ThreadId,但日志始终将其记录为 1。我有多个应用程序实例同时运行到同一个日志中,因此希望我可以根据 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();
Run Code Online (Sandbox Code Playgroud)
日志输出
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
Run Code Online (Sandbox Code Playgroud)
这对我有用 \xe2\x80\x94 它记录当前正在运行的线程的线程 ID,而不是创建 Logger 实例的线程 ID:
\n努吉特包
\n<PackageReference Include="Serilog.Enrichers.Thread" Version="3.1.0" />\nRun Code Online (Sandbox Code Playgroud)\nappsettings.json {ThreadId}
\n{\n "Logging": {\n "LogLevel": {\n "Default": "Information"\n }\n },\n "Serilog": {\n "MinimumLevel": {\n "Default": "Information",\n "Override": {\n "Microsoft": "Error"\n }\n },\n {\n "Name": "File",\n "Args": {\n "path": "/log/api.log",\n "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level}] Th:{ThreadId} {Message} {NewLine}{Exception}",\n "rollingInterval": "Day"\n }\n }\n ],\n "Properties": {\n "Application": "acme"\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n启动.Enrich.WithThreadId()
\npublic Startup(IConfiguration configuration)\n{\n Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).Enrich.WithThreadId()\n .CreateLogger();\n Configuration = configuration;\n}\nRun Code Online (Sandbox Code Playgroud)\n样本日志
\n2022-03-11 14:49:25.425 -05:00 [Information] Th:27 foo\n2022-03-11 14:49:25.428 -05:00 [Information] Th:5 Bar\nRun Code Online (Sandbox Code Playgroud)\n
好吧,我遵循@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();
Run Code Online (Sandbox Code Playgroud)
日志现在正在推出唯一的进程 ID
| 归档时间: |
|
| 查看次数: |
4893 次 |
| 最近记录: |