修改serilog 日志配置
parent
b5e05e9ec6
commit
e10477e25f
|
|
@ -1,4 +1,5 @@
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using Serilog.Sinks.Email;
|
using Serilog.Sinks.Email;
|
||||||
|
|
@ -13,40 +14,49 @@ namespace IRaCIS.Core.API
|
||||||
public static void AddSerilogSetup(string environment, IServiceProvider serviceProvider)
|
public static void AddSerilogSetup(string environment, IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
|
|
||||||
var config = new LoggerConfiguration()
|
Log.Logger = new LoggerConfiguration()
|
||||||
.MinimumLevel.Information()
|
.ReadFrom.Configuration(
|
||||||
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
|
new ConfigurationBuilder()
|
||||||
// Filter out ASP.NET Core infrastructre logs that are Information and below 日志太多了 一个请求 记录好几条
|
.AddJsonFile($"appsettings.{environment}.json")
|
||||||
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
|
.Build())
|
||||||
.MinimumLevel.Override("Hangfire", LogEventLevel.Warning)
|
//.Enrich.WithClientIp()
|
||||||
.MinimumLevel.Override("System.Net.Http.HttpClient.HttpReports", LogEventLevel.Warning)
|
//.Enrich.WithClientAgent()
|
||||||
.Enrich.WithClientIp()
|
.Enrich.FromLogContext()
|
||||||
.Enrich.WithClientAgent()
|
.Enrich.WithHttpContextInfo(serviceProvider)
|
||||||
.Enrich.FromLogContext()
|
.CreateLogger();
|
||||||
|
|
||||||
//控制台 方便调试 问题 我们显示记录日志 时 获取上下文的ip 和用户名 用户类型
|
|
||||||
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning,
|
|
||||||
outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} ] {LocalIP} {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine} {Exception}")
|
|
||||||
.WriteTo.File($"{AppContext.BaseDirectory}Serilogs/.log", rollingInterval: RollingInterval.Day,
|
|
||||||
outputTemplate: "{Timestamp:HH:mm:ss} || {Level} || {SourceContext:l} || {Message} ||{Exception} ||end {NewLine}");
|
|
||||||
//.WriteTo.MSSqlServer("Data Source=DESKTOP-4TU9A6M;Initial Catalog=CoreFrame;User ID=sa;Password=123456", "logs", autoCreateSqlTable: true, restrictedToMinimumLevel: LogEventLevel.Information)//从左至右四个参数分别是数据库连接字符串、表名、如果表不存在是否创建、最低等级。Serilog会默认创建一些列。
|
|
||||||
|
|
||||||
if (environment == "Production")
|
//Log.Information("Hello, world!");
|
||||||
{
|
|
||||||
config.WriteTo.Email(new EmailConnectionInfo()
|
|
||||||
{
|
|
||||||
EmailSubject = "系统警告,请速速查看!",//邮件标题
|
|
||||||
FromEmail = "iracis_grr@163.com",//发件人邮箱
|
|
||||||
MailServer = "smtp.163.com",//smtp服务器地址
|
|
||||||
NetworkCredentials = new NetworkCredential("iracis_grr@163.com", "XLWVQKZAEKLDWOAH"),//两个参数分别是发件人邮箱与客户端授权码
|
|
||||||
Port = 25,//端口号
|
|
||||||
ToEmail = "872297557@qq.com"//收件人
|
|
||||||
}, restrictedToMinimumLevel: LogEventLevel.Error,
|
|
||||||
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [ {Level} {ClientIp} {ClientAgent} {TokenUserRealName} {TokenUserType} ] || [path: {RequestPath} arguments: {RequestBody}] {SourceContext:l} || {Message} || {Exception} ||end {NewLine})");
|
|
||||||
}
|
|
||||||
|
|
||||||
//扩展方法 获取上下文的ip 用户名 用户类型
|
//Log.Information("About to process input: ...");
|
||||||
Log.Logger = config.Enrich.WithHttpContextInfo(serviceProvider).CreateLogger();
|
|
||||||
|
//Log.Verbose("Verbose - Ah, there you are!");
|
||||||
|
//Log.Debug("Debug - Ah, there you are!");
|
||||||
|
//Log.Information("Information - Ah, there you are!");
|
||||||
|
//Log.Warning("Warning - Ah, there you are!");
|
||||||
|
//Log.Error("Error - Ah, there you are!");
|
||||||
|
//Log.Fatal("Fatal - ErrorAh, there you are!");
|
||||||
|
|
||||||
|
//var config = new LoggerConfiguration()
|
||||||
|
// .MinimumLevel.Information()
|
||||||
|
// .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("Hangfire", LogEventLevel.Warning)
|
||||||
|
// .MinimumLevel.Override("System.Net.Http.HttpClient.HttpReports", LogEventLevel.Warning)
|
||||||
|
// .Enrich.WithClientIp()
|
||||||
|
// .Enrich.WithClientAgent()
|
||||||
|
// .Enrich.FromLogContext()
|
||||||
|
|
||||||
|
// //控制台 方便调试 问题 我们显示记录日志 时 获取上下文的ip 和用户名 用户类型
|
||||||
|
// .WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Warning,
|
||||||
|
// outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} ] {LocalIP} {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine} {Exception}")
|
||||||
|
// .WriteTo.File($"{AppContext.BaseDirectory}Serilogs/.log", rollingInterval: RollingInterval.Day,
|
||||||
|
// outputTemplate: "{Timestamp:HH:mm:ss} || {Level} || {SourceContext:l} || {Message} ||{Exception} ||end {NewLine}");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
////扩展方法 获取上下文的ip 用户名 用户类型
|
||||||
|
//Log.Logger = config.Enrich.WithHttpContextInfo(serviceProvider).CreateLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,39 @@
|
||||||
{
|
{
|
||||||
"Logging": {
|
"Serilog": {
|
||||||
"LogLevel": {
|
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.ClientInfo" ],
|
||||||
|
"MinimumLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft": "Warning",
|
"Override": {
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft": "Information",
|
||||||
}
|
"Microsoft.AspNetCore": "Warning",
|
||||||
|
"Hangfire": "Warning",
|
||||||
|
"Quartz": "Warning",
|
||||||
|
"System.Net.Http.HttpClient.HttpReports": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Enrich": [ "FromLogContext", "WithClientIp", "WithClientAgent" ],
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "Console",
|
||||||
|
"Args": {
|
||||||
|
"restrictedToMinimumLevel": "Warning",
|
||||||
|
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {LocalIP} {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine}{Exception}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Serilogs/log-.log",
|
||||||
|
"rollingInterval": "Hour",
|
||||||
|
"rollOnFileSizeLimit": true,
|
||||||
|
"fileSizeLimitBytes": 1048576,
|
||||||
|
"retainedFileCountLimit": 61320,
|
||||||
|
"outputTemplate": "{Timestamp:HH:mm:ss} || {Level} || {SourceContext:l} || {Message} ||{Exception} ||end {NewLine}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
|
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"RemoteNew": "Server=192.168.3.68,1434;Database=LiLi_DB;User ID=Lili_User;Password=Eleveta@2025;TrustServerCertificate=true"
|
"RemoteNew": "Server=192.168.3.68,1434;Database=LiLi_DB;User ID=Lili_User;Password=Eleveta@2025;TrustServerCertificate=true"
|
||||||
},
|
},
|
||||||
|
|
@ -28,4 +56,4 @@
|
||||||
"AuthorizationCode": "Q#669869497420ul"
|
"AuthorizationCode": "Q#669869497420ul"
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,37 @@
|
||||||
{
|
{
|
||||||
"Logging": {
|
"Serilog": {
|
||||||
"LogLevel": {
|
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.ClientInfo" ],
|
||||||
|
"MinimumLevel": {
|
||||||
"Default": "Information",
|
"Default": "Information",
|
||||||
"Microsoft": "Warning",
|
"Override": {
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft": "Information",
|
||||||
}
|
"Microsoft.AspNetCore": "Warning",
|
||||||
|
"Hangfire": "Warning",
|
||||||
|
"Quartz": "Warning",
|
||||||
|
"System.Net.Http.HttpClient.HttpReports": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Enrich": [ "FromLogContext", "WithClientIp", "WithClientAgent" ],
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "Console",
|
||||||
|
"Args": {
|
||||||
|
"restrictedToMinimumLevel": "Warning",
|
||||||
|
"outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {LocalIP} {ClientIp} {TokenUserRealName} {TokenUserType} {Message:lj} {Properties:j}{NewLine}{Exception}"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "Serilogs/log-.txt",
|
||||||
|
"rollingInterval": "Hour",
|
||||||
|
"rollOnFileSizeLimit": true,
|
||||||
|
"fileSizeLimitBytes": 1048576,
|
||||||
|
"retainedFileCountLimit": 61320,
|
||||||
|
"outputTemplate": "{Timestamp:HH:mm:ss} || {Level} || {SourceContext:l} || {Message} ||{Exception} ||end {NewLine}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"RemoteNew": "Server=192.168.3.68,1434;Database=Test_510K;User ID=sa;Password=xc@123456;TrustServerCertificate=true"
|
"RemoteNew": "Server=192.168.3.68,1434;Database=Test_510K;User ID=sa;Password=xc@123456;TrustServerCertificate=true"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue