Explorar o código

优化日志配置,新增 Serilog 和 MongoDB 支持

在 `Program.cs` 中:
- 引入 `Serilog.Formatting.Display`,调整日志初始化逻辑。
- 使用 `builder.Services.AddSerilog` 配置日志,支持从配置和服务中读取设置,并增强日志上下文。
- 删除无用的 `AddControllers().AddNewtonsoftJson()` 代码。

在 `appsettings.shared.Development.json` 中:
- 删除旧的 MongoDB 日志配置注释。
- 添加新的 MongoDB 日志配置,设置数据库地址、集合名称及日志存储限制。

这些更改改进了日志记录的初始化和存储配置。
xf hai 4 días
pai
achega
d092f4e996

+ 14 - 6
src/Hotline.Api/Program.cs

@@ -1,5 +1,6 @@
 using Hotline.Api;
 using Serilog;
+using Serilog.Formatting.Display;
 using XF.Domain.Authentications;
 
 Log.Logger = new LoggerConfiguration()
@@ -34,13 +35,20 @@ try
                 .AddCommandLine(args)
                 ;
         })
-        .UseSerilog((ctx, lc) => lc
-        //.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
-        .Enrich.FromLogContext()
-        .ReadFrom.Configuration(ctx.Configuration), true)
+        //.UseSerilog((ctx, lc) => lc
+        ////.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level}] {SourceContext}{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}")
+        //.Enrich.FromLogContext()
+        //.ReadFrom.Configuration(ctx.Configuration), true)
         ;
-    //builder.Services.AddControllers().AddNewtonsoftJson();
-	builder
+    
+    builder.Services.AddSerilog((services, lc) => lc
+        .ReadFrom.Configuration(builder.Configuration)
+        .ReadFrom.Services(services)
+        .Enrich.FromLogContext()
+    );
+
+
+    builder
         .ConfigureServices()
         .ConfigurePipelines()
         .Run();

+ 10 - 11
src/Hotline.Api/config/appsettings.shared.Development.json

@@ -58,17 +58,16 @@
       //    "outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext} [{TraceId}]{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}",
       //  }
       //},
-      //{
-      //  "Name": "MongoDBBson",
-      //  "Args": {
-      //    "databaseUrl": "mongodb://dev:fw123kj@110.188.24.182:57017/hotline_log?authSource=admin",
-      //    "collectionName": "logs",
-      //    "cappedMaxSizeMb": "1024",
-      //    "cappedMaxDocuments": "50000",
-      //    "rollingInterval": "Month",
-      //    "outputTemplate": "[{Timestamp:HH:mm:ss} {Level}] {SourceContext} [{TraceId}]{NewLine}{Message:lj}{NewLine}{Exception}{NewLine}"
-      //  }
-      //}
+      {
+        "Name": "MongoDBBson",
+        "Args": {
+          "databaseUrl": "mongodb://dev:fw123kj@110.188.24.182:57017/123_log?authSource=admin",
+          "collectionName": "logs",
+          "cappedMaxSizeMb": "1024",
+          "cappedMaxDocuments": "50000",
+          "rollingInterval": "Month"
+        }
+      }
     ],
     "Enrich": [ "FromLogContext", "WithSpan" ]
   }