• Serilog


    1,引用包

        <PackageReference Include="Serilog.Extensions.Hosting" Version="3.1.0" />
        <PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
        <PackageReference Include="Serilog.Sinks.Console" Version="3.1.0" />
        <PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />

    2,初始化

            static async Task Main(string[] args)
            {
                Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Information()
                .MinimumLevel.Override("ProjectService", LogEventLevel.Error)//当前log类型的输出等级为error
                .Enrich.With(new ThreadIdEnrichar())//新增ThreadId字段
                .Enrich.FromLogContext()
                .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {ThreadId}] {Message}{NewLine}{Exception}")//设置格式
                //.WriteTo.Async(c => c.File("Log\logs.txt", rollingInterval: RollingInterval))
                .WriteTo.File("logs\myapp.txt", rollingInterval: RollingInterval.Day, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3} {ThreadId}] {Message}{NewLine}{Exception}")
                .CreateLogger();
    
                string[] s = new string[] { "1", "2" };
                Log.Information("{@s}", s);
    
                await CreateHostBuilder(args).RunConsoleAsync();
                Console.WriteLine("Hello World!");
            }
    ThreadIdEnrichar
    using Serilog.Core;
    using Serilog.Events;
    using System.Threading;
    
    public class ThreadIdEnrichar : ILogEventEnricher
    {
        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            logEvent.AddPropertyIfAbsent(propertyFactory.CreateProperty("ThreadId", Thread.CurrentThread.ManagedThreadId));
        }
    }
  • 相关阅读:
    NOP(4) default
    NOP(三) ASP.NET Application Life Cycle
    About the IoC
    开园庆祝!
    js 添加/删除数组开头/结尾元素
    JavaScript String.prototype.slice()
    JavaScript Array.prototype.splice()方法的使用
    js Map
    js Set
    Bruteforce Algorithm [HDU 3221]
  • 原文地址:https://www.cnblogs.com/zd1994/p/14190208.html
Copyright © 2020-2023  润新知