.net5 Serilog使用

一,安装包 Serilog.AspNetCore

  1,startup.cs 添加  // Serilog请求日志中间件---必须在 UseStaticFiles 和 UseRouting 之间

 app.UseSerilogRequestLogging();

 2,方式一  在program.cs 

static string LogFilePath(string LogEvent) => $@"{AppContext.BaseDirectory}\Logs\{LogEvent}\log.log";
static string SerilogOutputTemplate = "【时间】{Timestamp:yyyy-MM-dd HH:mm:ss,fff}{NewLine}【等级】{Level:u3}{NewLine}【消息】{Message:lj}{NewLine}{Exception}{NewLine}";

在CreateHostBuilder 中添加

 

.UseSerilog((context, logger) =>
{

logger.Enrich.FromLogContext();
logger.WriteTo.Console();//输出到控制台
logger.MinimumLevel.Debug();// 所有Sink的最小记录级别
logger.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Debug).WriteTo.File(LogFilePath("Debug"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)).WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Information).WriteTo.File(LogFilePath("Information"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)).WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Warning).WriteTo.File(LogFilePath("Warning"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)).WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Error).WriteTo.File(LogFilePath("Error"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate)).WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(p => p.Level == LogEventLevel.Fatal).WriteTo.File(LogFilePath("Fatal"), rollingInterval: RollingInterval.Day, outputTemplate: SerilogOutputTemplate));
});

3,方式二 读取配置模板

.UseSerilog((context, logger) =>
        {
            logger.ReadFrom.Configuration(context.Configuration);
            logger.Enrich.FromLogContext();
            logger.WriteTo.Console();
     });

appsettings.json

{ 
  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
          "Default": "Warning",
        "System": "Warning",
        "Microsoft": "Warning"
      }
    },
    "WriteTo": [
      { "Name": "Console" }, //输出到控制台
      {
        "Name": "Async", //异步写入日志
        "Args": {
          "configure": [
            {
              "Name": "File", //输出文件
              "Args": {
                "path": "logs/.log",
                "outputTemplate": "{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff}{NewLine}LogLevel:{Level}{NewLine}Class:{SourceContext}{NewLine}Message:{Message}{NewLine}{Exception}",
                "rollingInterval": "3" //日志文件生成精度:1:年  2:月 3:日 4:小时
              }
            }
          ]
        }
      }
      //{
      //  "Name": "Console",
      //  "Args": {
      //    "outputTemplate": "时间{Timestamp:yyyy-MM-dd HH:mm:ss,fff}{NewLine}等级{Level:u3}{NewLine}消息{Message:lj}{NewLine}{NewLine}"
      //  }
      //},
      //{
      //  "Name": "File",
      //  "Args": {
      //    "path": "logs/.log",
      //    "rollingInterval": "Day",
      //    "outputTemplate": "【时间】{Timestamp:yyyy-MM-dd HH:mm:ss,fff}{NewLine}【等级】{Level:u3}{NewLine}【消息】{Message:lj}{NewLine}{NewLine}"
      //  }
      //}
    ]
  }

 

"Serilog": {
    "MinimumLevel": "Information",
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs\\log.log",
          "rollingInterval": "Day",
          "retainedFileCountLimit": "2",
          "retainedFileTimeLimit": "2.00:00:00", //Deletes files older than 2 days
          "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}"
        }
      }
    ]
  }

 

posted @ 2023-01-16 15:37  刘小吉  阅读(212)  评论(0编辑  收藏  举报