测试修改serilog 日志
parent
8bd82ed409
commit
f7e47293dd
|
@ -340,13 +340,10 @@
|
||||||
序列化,反序列化的时候,处理时间 时区转换
|
序列化,反序列化的时候,处理时间 时区转换
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:IRaCIS.Core.API.NullToEmptyStringResolver.CreateProperties(System.Type,Newtonsoft.Json.MemberSerialization)">
|
<member name="T:IRaCIS.Core.API.NullToEmptyStringResolver">
|
||||||
<summary>
|
<summary>
|
||||||
创建属性
|
LowerCamelCaseJsonAttribute 可以设置类小写返回给前端
|
||||||
</summary>
|
</summary>
|
||||||
<param name="type">类型</param>
|
|
||||||
<param name="memberSerialization">序列化成员</param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
</member>
|
||||||
<member name="T:ZhaoXi._001.NET5Demo.Practice.WebApi.Utility.Jwt.CustomHSJWTService">
|
<member name="T:ZhaoXi._001.NET5Demo.Practice.WebApi.Utility.Jwt.CustomHSJWTService">
|
||||||
<summary>
|
<summary>
|
||||||
|
|
|
@ -80,6 +80,7 @@ builder.Services.AddExceptionHandler<GlobalExceptionHandler>();
|
||||||
|
|
||||||
//健康检查
|
//健康检查
|
||||||
builder.Services.AddHealthChecks();
|
builder.Services.AddHealthChecks();
|
||||||
|
builder.Services.AddSerilog();
|
||||||
//本地化
|
//本地化
|
||||||
builder.Services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
|
builder.Services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
using IRaCIS.Core.API._PipelineExtensions.Serilog;
|
using IRaCIS.Core.API._PipelineExtensions.Serilog;
|
||||||
|
using IRaCIS.Core.Domain.Share;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
|
||||||
namespace IRaCIS.Core.API
|
namespace IRaCIS.Core.API
|
||||||
|
@ -17,8 +19,52 @@ namespace IRaCIS.Core.API
|
||||||
app.UseSerilogRequestLogging(opts
|
app.UseSerilogRequestLogging(opts
|
||||||
=>
|
=>
|
||||||
{
|
{
|
||||||
|
|
||||||
opts.MessageTemplate = "{TokenUserRealName} {TokenUserTypeShortName} {ClientIp} {LocalIP} {Host} {Protocol} {RequestMethod} {RequestPath} {RequestBody} responded {StatusCode} in {Elapsed:0.0000} ms";
|
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);
|
||||||
|
}
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -16,18 +16,25 @@ namespace IRaCIS.Core.API
|
||||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
||||||
// Filter out ASP.NET Core infrastructre logs that are Information and below 日志太多了 一个请求 记录好几条
|
// Filter out ASP.NET Core infrastructre logs that are Information and below 日志太多了 一个请求 记录好几条
|
||||||
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
.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("Hangfire", LogEventLevel.Warning)
|
||||||
.MinimumLevel.Override("System.Net.Http.HttpClient.HttpReports", LogEventLevel.Warning)
|
|
||||||
.Enrich.WithClientIp()
|
.Enrich.WithClientIp()
|
||||||
|
.Enrich.WithRequestHeader("User-Agent")
|
||||||
.Enrich.FromLogContext()
|
.Enrich.FromLogContext()
|
||||||
.Filter.ByExcluding(logEvent => logEvent.Properties.ContainsKey("RequestPath") && logEvent.Properties["RequestPath"].ToString().Contains("/health"))
|
.Filter.ByExcluding(logEvent => logEvent.Properties.ContainsKey("RequestPath") && logEvent.Properties["RequestPath"].ToString().Contains("/health"))
|
||||||
|
|
||||||
//控制台 方便调试 问题 我们显示记录日志 时 获取上下文的ip 和用户名 用户类型
|
//控制台 方便调试 问题 我们显示记录日志 时 获取上下文的ip 和用户名 用户类型
|
||||||
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning,
|
.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,
|
.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会默认创建一些列。
|
//.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 用户名 用户类型
|
//扩展方法 获取上下文的ip 用户名 用户类型
|
||||||
Log.Logger = config.Enrich.WithHttpContextInfo(serviceProvider).CreateLogger();
|
//Log.Logger = config.Enrich.WithHttpContextInfo(serviceProvider).CreateLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue