zoukankan      html  css  js  c++  java
  • 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));
        }
    }
  • 相关阅读:
    php单元测试
    git配置本地环境(phpstudy/tortoisegit/git等)
    xp/win7中系统安装memcached服务,卸载memcached服务,以及删除memcached服务
    memcached装、启动和卸载
    如何安装memcached
    三元运算符、逻辑运算符
    移动端页面怎么适配ios页面
    javascript正则表达式——元字符
    一个div添加多个背景图片
    GNU Screen使用入门
  • 原文地址:https://www.cnblogs.com/zd1994/p/14190208.html
Copyright © 2011-2022 走看看