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();
                });
  • 相关阅读:
    理解-NumPy
    python % format
    计算机三级网络技术考过指南
    jquery 增加与删除数组元素
    jquery 改变标签可见状态的几种方式
    pL/SQL 创建DBLIKN
    Oracle 数据类型详解
    状态模式例子---流程控制
    jqGrid 将行的字变成超连接
    表单提交详细介绍
  • 原文地址:https://www.cnblogs.com/IWings/p/13035513.html
Copyright © 2011-2022 走看看