diff --git a/IRaCIS.Core.API/IRaCIS.Core.API.xml b/IRaCIS.Core.API/IRaCIS.Core.API.xml index 216f82c9a..4b4186d94 100644 --- a/IRaCIS.Core.API/IRaCIS.Core.API.xml +++ b/IRaCIS.Core.API/IRaCIS.Core.API.xml @@ -340,13 +340,10 @@ 序列化,反序列化的时候,处理时间 时区转换 - + - 创建属性 + LowerCamelCaseJsonAttribute 可以设置类小写返回给前端 - 类型 - 序列化成员 - diff --git a/IRaCIS.Core.API/Progranm.cs b/IRaCIS.Core.API/Progranm.cs index 419c98313..0863231cd 100644 --- a/IRaCIS.Core.API/Progranm.cs +++ b/IRaCIS.Core.API/Progranm.cs @@ -80,6 +80,7 @@ builder.Services.AddExceptionHandler(); //健康检查 builder.Services.AddHealthChecks(); +builder.Services.AddSerilog(); //本地化 builder.Services.AddJsonLocalization(options => options.ResourcesPath = "Resources"); @@ -197,7 +198,7 @@ app.UseExceptionHandler(o => { }); app.UseIRacisHostStaticFileStore(env); //本地化 - await app.UseLocalization(app.Services); +await app.UseLocalization(app.Services); app.UseForwardedHeaders(); diff --git a/IRaCIS.Core.API/_PipelineExtensions/Serilog/SerilogConfig.cs b/IRaCIS.Core.API/_PipelineExtensions/Serilog/SerilogConfig.cs index e4f418057..79496bf3e 100644 --- a/IRaCIS.Core.API/_PipelineExtensions/Serilog/SerilogConfig.cs +++ b/IRaCIS.Core.API/_PipelineExtensions/Serilog/SerilogConfig.cs @@ -1,6 +1,8 @@ using IRaCIS.Core.API._PipelineExtensions.Serilog; +using IRaCIS.Core.Domain.Share; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; using Serilog; namespace IRaCIS.Core.API @@ -17,8 +19,52 @@ namespace IRaCIS.Core.API app.UseSerilogRequestLogging(opts => { + opts.MessageTemplate = "{TokenUserRealName} {TokenUserTypeShortName} {ClientIp} {LocalIP} {Host} {Protocol} {RequestMethod} {RequestPath} {RequestBody} responded {StatusCode} in {Elapsed:0.0000} ms"; - opts.EnrichDiagnosticContext = SerilogHelper.EnrichFromRequest; + + opts.EnrichDiagnosticContext = (diagnosticContext, httpContext) => + { + + var request = httpContext.Request; + + // Set all the common properties available for every request + diagnosticContext.Set("Host", request.Host); + + diagnosticContext.Set("Protocol", request.Protocol); + diagnosticContext.Set("Scheme", request.Scheme); + + #region old 未用 + //这种获取的Ip不准 配置服务才行 + //diagnosticContext.Set("RequestIP", httpContext.Connection.RemoteIpAddress.ToString()); + + //这种方式可以,但是serilog提供了 就不用了 + //diagnosticContext.Set("TestIP", httpContext.GetUserIp()); + + //这种方式不行 读取的body为空字符串 必须在中间件中读取 + //diagnosticContext.Set("RequestBody", await ReadRequestBody(httpContext.Request)); + //diagnosticContext.Set("RequestBody", RequestPayload); + #endregion + + // Only set it if available. You're not sending sensitive data in a querystring right?! + if (request.QueryString.HasValue) + { + diagnosticContext.Set("QueryString", request.QueryString.Value); + } + + // Set the content-type of the Response at this point + diagnosticContext.Set("ContentType", httpContext.Response.ContentType); + + diagnosticContext.Set("TokenUserRealName", httpContext?.User?.FindFirst(JwtIRaCISClaimType.RealName)?.Value); + + diagnosticContext.Set("TokenUserTypeShortName", httpContext?.User?.FindFirst(JwtIRaCISClaimType.UserTypeShortName)?.Value); + + // Retrieve the IEndpointFeature selected for the request + var endpoint = httpContext.GetEndpoint(); + if (endpoint is object) // endpoint != null + { + diagnosticContext.Set("EndpointName", endpoint.DisplayName); + } + }; }); diff --git a/IRaCIS.Core.API/_PipelineExtensions/Serilog/SerilogHelper.cs b/IRaCIS.Core.API/_PipelineExtensions/Serilog/SerilogHelper.cs deleted file mode 100644 index 7df3c4267..000000000 --- a/IRaCIS.Core.API/_PipelineExtensions/Serilog/SerilogHelper.cs +++ /dev/null @@ -1,56 +0,0 @@ -using IRaCIS.Core.Domain.Share; -using Microsoft.AspNetCore.Http; -using Serilog; - -namespace IRaCIS.Core.API -{ - public class SerilogHelper - { - //public static string RequestPayload = ""; - - public static void EnrichFromRequest(IDiagnosticContext diagnosticContext, HttpContext httpContext) - { - var request = httpContext.Request; - - // Set all the common properties available for every request - diagnosticContext.Set("Host", request.Host); - - diagnosticContext.Set("Protocol", request.Protocol); - diagnosticContext.Set("Scheme", request.Scheme); - - #region old 未用 - //这种获取的Ip不准 配置服务才行 - //diagnosticContext.Set("RequestIP", httpContext.Connection.RemoteIpAddress.ToString()); - - //这种方式可以,但是serilog提供了 就不用了 - //diagnosticContext.Set("TestIP", httpContext.GetUserIp()); - - //这种方式不行 读取的body为空字符串 必须在中间件中读取 - //diagnosticContext.Set("RequestBody", await ReadRequestBody(httpContext.Request)); - //diagnosticContext.Set("RequestBody", RequestPayload); - #endregion - - // Only set it if available. You're not sending sensitive data in a querystring right?! - if (request.QueryString.HasValue) - { - diagnosticContext.Set("QueryString", request.QueryString.Value); - } - - // Set the content-type of the Response at this point - diagnosticContext.Set("ContentType", httpContext.Response.ContentType); - - diagnosticContext.Set("TokenUserRealName", httpContext?.User?.FindFirst(JwtIRaCISClaimType.RealName)?.Value); - - diagnosticContext.Set("TokenUserTypeShortName", httpContext?.User?.FindFirst(JwtIRaCISClaimType.UserTypeShortName)?.Value); - - // Retrieve the IEndpointFeature selected for the request - var endpoint = httpContext.GetEndpoint(); - if (endpoint is object) // endpoint != null - { - diagnosticContext.Set("EndpointName", endpoint.DisplayName); - } - } - - - } -} diff --git a/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs b/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs index b7576f849..df89d5457 100644 --- a/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs +++ b/IRaCIS.Core.API/_ServiceExtensions/Serilog/SerilogSetup.cs @@ -16,18 +16,25 @@ namespace IRaCIS.Core.API .MinimumLevel.Override("Microsoft", LogEventLevel.Information) // Filter out ASP.NET Core infrastructre logs that are Information and below 日志太多了 一个请求 记录好几条 .MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.AspNetCore.Hosting", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.AspNetCore.Mvc", LogEventLevel.Warning) + .MinimumLevel.Override("Microsoft.AspNetCore.Routing", LogEventLevel.Warning) + .MinimumLevel.Override("Hangfire", LogEventLevel.Warning) - .MinimumLevel.Override("System.Net.Http.HttpClient.HttpReports", LogEventLevel.Warning) .Enrich.WithClientIp() + .Enrich.WithRequestHeader("User-Agent") .Enrich.FromLogContext() .Filter.ByExcluding(logEvent => logEvent.Properties.ContainsKey("RequestPath") && logEvent.Properties["RequestPath"].ToString().Contains("/health")) //控制台 方便调试 问题 我们显示记录日志 时 获取上下文的ip 和用户名 用户类型 .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning, - outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext:l} || {Message} || {Exception} ||end {NewLine}") + outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {ClientIp} {SourceContext:l} || {Message} || {Exception} ||end {NewLine}") .WriteTo.File($"{AppContext.BaseDirectory}Serilogs/.log", rollingInterval: RollingInterval.Day, - outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {SourceContext:l} || {Message} || {Exception} ||end {NewLine}"); + outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {ClientIp} {SourceContext:l} || {Message} || {Exception} ||end {NewLine}"); + + + Log.Logger = config.CreateLogger(); //.WriteTo.MSSqlServer("Data Source=DESKTOP-4TU9A6M;Initial Catalog=CoreFrame;User ID=sa;Password=123456", "logs", autoCreateSqlTable: true, restrictedToMinimumLevel: LogEventLevel.Information)//从左至右四个参数分别是数据库连接字符串、表名、如果表不存在是否创建、最低等级。Serilog会默认创建一些列。 @@ -46,7 +53,7 @@ namespace IRaCIS.Core.API //} //扩展方法 获取上下文的ip 用户名 用户类型 - Log.Logger = config.Enrich.WithHttpContextInfo(serviceProvider).CreateLogger(); + //Log.Logger = config.Enrich.WithHttpContextInfo(serviceProvider).CreateLogger(); } }