.net core 使用 Serilog 作为日志提供者

nuget引入 Serilog.AspNetCore

Startup构造函数:

 

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
         // 最小的日志输出级别
         //.MinimumLevel.Error()
         // 日志调用类命名空间如果以 Microsoft 开头,覆盖日志输出最小级别为 Information
         //.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
         .Enrich.FromLogContext()
         // 配置日志输出到控制台
#if DEBUG .WriteTo.Console()
#endif
// 配置日志输出到文件,文件输出到当前项目的 logs 目录下 .WriteTo.Logger(lc => { lc.Filter.ByExcluding(e => e.Level == LogEventLevel.Error || e.Level == LogEventLevel.Debug) .WriteTo.File("logs/normal/log.txt", outputTemplate: "{Timestamp:HH:mm:ss} [{Level}] {SourceContext} {NewLine}{Message}{NewLine}{Exception}", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true); }) .WriteTo.Logger(lc => { lc.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error) .WriteTo.File("logs/errors/log.txt", outputTemplate: "{Timestamp:HH:mm:ss} [{Level}] {SourceContext} {NewLine}{Message}{NewLine}{Exception}", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true); }) .WriteTo.Logger(lc => { lc.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug) .WriteTo.File("logs/debugs/log.txt", outputTemplate: "{Timestamp:HH:mm:ss} [{Level}] {SourceContext} {NewLine}{Message}{NewLine}{Exception}", rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true); }) // 创建 logger .CreateLogger(); }

 

 对某个SouceContext特定一个文件:

               .WriteTo.Logger(lc =>
               {
                   lc.Filter.ByIncludingOnly(e => {
                       if (e.Level == LogEventLevel.Error)
                       {
                           var sc = e.Properties["SourceContext"];
                           if (sc != null && sc.ToString() == "\"JMS.JMSClient\"")
                               return true;
                       }
                       return false;
                   })
                   .WriteTo.File("logs/JMSClient Error/log.txt",
                    outputTemplate: "{Timestamp:HH:mm:ss} {NewLine}{Message}{NewLine}{Exception}",
                       rollingInterval: RollingInterval.Day,
                       rollOnFileSizeLimit: true);
               })

 

 

CreateHostBuilder 里面,UseSerilog

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
           .UseSerilog()
            .UseServiceProviderFactory(new JackAspNetCoreServiceProviderFactory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
        }

 appsettings设置

  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "System": "Warning",
        "Microsoft": "Warning"
      }
    }
  }

 控制台程序使用Serilog

nuget 引入:

Serilog.Extensions.Logging
Serilog.Settings.Configuration
Serilog.Sinks.Console
Serilog.Sinks.File

配置代码和上面的startup一样,然后在ServiceCollection里激活serilog

            services.AddLogging(builder => {
                builder.AddSerilog();
            });

 

posted @ 2020-06-03 09:40  IWing  阅读(833)  评论(0编辑  收藏  举报