修改serilog 日志配置
parent
b5e05e9ec6
commit
e10477e25f
|
|
@ -1,4 +1,5 @@
|
|||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using Serilog.Sinks.Email;
|
||||
|
|
@ -13,40 +14,49 @@ namespace IRaCIS.Core.API
|
|||
public static void AddSerilogSetup(string environment, IServiceProvider serviceProvider)
|
||||
{
|
||||
|
||||
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()
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(
|
||||
new ConfigurationBuilder()
|
||||
.AddJsonFile($"appsettings.{environment}.json")
|
||||
.Build())
|
||||
//.Enrich.WithClientIp()
|
||||
//.Enrich.WithClientAgent()
|
||||
.Enrich.FromLogContext()
|
||||
.Enrich.WithHttpContextInfo(serviceProvider)
|
||||
.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会默认创建一些列。
|
||||
//Log.Information("Hello, world!");
|
||||
|
||||
if (environment == "Production")
|
||||
{
|
||||
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})");
|
||||
}
|
||||
//Log.Information("About to process input: ...");
|
||||
|
||||
//扩展方法 获取上下文的ip 用户名 用户类型
|
||||
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": {
|
||||
"LogLevel": {
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.ClientInfo" ],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
"Override": {
|
||||
"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": {
|
||||
"RemoteNew": "Server=192.168.3.68,1434;Database=LiLi_DB;User ID=Lili_User;Password=Eleveta@2025;TrustServerCertificate=true"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,10 +1,37 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Enrichers.ClientInfo" ],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
"Override": {
|
||||
"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": {
|
||||
"RemoteNew": "Server=192.168.3.68,1434;Database=Test_510K;User ID=sa;Password=xc@123456;TrustServerCertificate=true"
|
||||
|
|
|
|||
Loading…
Reference in New Issue