1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| public class Program { //获取配置文件信息 public static IConfiguration Configuration { get; } = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", false, true) .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", false, true) .AddEnvironmentVariables() .Build(); public static void Main(string[] args) { //以json格式输出日志 //Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration) // .MinimumLevel.Debug() // .Enrich.FromLogContext() // .WriteTo.Console(new RenderedCompactJsonFormatter()) // .WriteTo.File(formatter: new CompactJsonFormatter(), "logs\\myapp-json.txt", // rollingInterval: RollingInterval.Day) // .CreateLogger();
//以默认格式输出 Log.Logger = new LoggerConfiguration().ReadFrom.Configuration(Configuration) .MinimumLevel.Debug() //把Microsoft的日志级别重写成Information .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.Console() .WriteTo.File(Path.Combine("logs", @"log.txt"), rollingInterval: RollingInterval.Day) .CreateLogger(); try { Log.Information("Starting web host"); CreateHostBuilder(args).Build().Run(); } catch (Exception e) { Log.Fatal(e, "Host terminated error"); Console.WriteLine(e); throw; } finally { Log.CloseAndFlush(); }
}
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }) .UseSerilog(dispose:true); }
|