54 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			C#
		
	
	
| using Microsoft.AspNetCore.Builder;
 | |
| using Serilog;
 | |
| using Serilog.Events;
 | |
| using Serilog.Sinks.Email;
 | |
| using System;
 | |
| using System.Net;
 | |
| 
 | |
| namespace IRaCIS.Core.API
 | |
| {
 | |
|     public class SerilogExtension
 | |
|     {
 | |
| 
 | |
|         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()
 | |
|                    
 | |
|                    //控制台 方便调试 问题   我们显示记录日志 时 获取上下文的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")
 | |
|             {
 | |
|                 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.Logger = config.Enrich.WithHttpContextInfo(serviceProvider).CreateLogger();
 | |
|         }
 | |
| 
 | |
|     }
 | |
| }
 |