zoukankan      html  css  js  c++  java
  • .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();
                });
  • 相关阅读:
    BZOJ3992 [SDOI2015]序列统计 【生成函数 + 多项式快速幂】
    BZOJ3993 [SDOI2015]星际战争 【二分 + 网络流】
    BZOJ3325 [Scoi2013]密码 【manacher】
    BZOJ3534 [Sdoi2014]重建 【矩阵树定理】
    BZOJ3507 [Cqoi2014]通配符匹配 【哈希 + 贪心】
    BZOJ2285 [SDOI2011]保密 【01分数规划 + 网络流】
    BZOJ4556 [Tjoi2016&Heoi2016]字符串 【后缀数组 + 主席树 + 二分 + ST表】
    BZOJ4817 [Sdoi2017]树点涂色 【LCT + 线段树】
    BZOJ1195 [HNOI2006]最短母串 【状压dp】
    malloc的使用、用malloc动态分配内存以适应用户的需求的源代码实例
  • 原文地址:https://www.cnblogs.com/IWings/p/13035513.html
Copyright © 2011-2022 走看看