• serilog .net core 3.1 配置方式


    serilog .net core 3.1 配置方式

    安装包dotnet add package Serilog dotnet add package Serilog.AspNetCore

    早初始化模式

        public class Program
        {
            public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();
    
            public static int Main(string[] args)
            {
                Log.Logger = new LoggerConfiguration()
                    .ReadFrom.Configuration(Configuration)
                    .Enrich.FromLogContext()
                    .WriteTo.Debug()
                    .WriteTo.Console(
                        outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}")
                    .CreateLogger();
    
                try
                {
                    Log.Information("Getting the motors running...");
    
                    CreateHostBuilder(args).Build().Run();
    
                    return 0;
                }
                catch (Exception ex)
                {
                    Log.Fatal(ex, "Host terminated unexpectedly");
                    return 1;
                }
                finally
                {
                    Log.CloseAndFlush();
                }
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                    })
                    .UseSerilog();
        }
    
    

    内联初始化

        public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>())
                    .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                        .ReadFrom.Configuration(hostingContext.Configuration)
                        .Enrich.FromLogContext()
                        .WriteTo.Debug()
                        .WriteTo.Console(
                            outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj} {Properties:j}{NewLine}{Exception}"));
        }
    
  • 相关阅读:
    Selenium RC For Python:教程2
    sql server 获取同比或环比
    C#泛型泛型集合Dictionary<K,V>
    C# FileStream
    c# 强命名的作用
    装箱与拆箱的概念及意义
    SQL Server中Rollup关键字使用技巧
    Javascript内存泄露
    几条复杂的SQL语句
    Group by与having理解
  • 原文地址:https://www.cnblogs.com/WNpursue/p/13029134.html
Copyright © 2020-2023  润新知