You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.7 KiB
62 lines
1.7 KiB
3 weeks ago
|
using Microsoft.AspNetCore.Hosting;
|
||
|
using Microsoft.Extensions.Hosting;
|
||
|
using Serilog;
|
||
|
using System;
|
||
|
|
||
|
namespace Wood.Admin.WebApi
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// program
|
||
|
/// </summary>
|
||
|
public class Program
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// main
|
||
|
/// </summary>
|
||
|
/// <param name="args"></param>
|
||
|
public static void Main(string[] args)
|
||
|
{
|
||
|
// 创建并配置Serilog Logger
|
||
|
Log.Logger = new LoggerConfiguration()
|
||
|
.MinimumLevel.Information() // 设置最低日志级别
|
||
|
.Enrich.FromLogContext() // 增加上下文信息
|
||
|
.WriteTo.Console(outputTemplate: "{Timestamp:HH:mm:ss} [{Level:u3}] {Message:lj}{NewLine}{Exception}") // 输出到控制台模板
|
||
|
.WriteTo.File("logs\\log.txt",
|
||
|
rollingInterval: RollingInterval.Day,// 按天分割,每天滚动创建新的日志文件
|
||
|
retainedFileCountLimit: 90,// 只保存最近90天的日志
|
||
|
fileSizeLimitBytes: 10_485_760, // 文件大小限制为10MB (10 * 1024 * 1024 bytes)
|
||
|
rollOnFileSizeLimit: true, // 达到文件大小限制时滚动
|
||
|
outputTemplate: "{NewLine}{NewLine}Date:{Timestamp:yyyy-MM-dd HH:mm:ss.fff} LogLevel:{Level}{NewLine}{Message}{NewLine}" + new string('-', 50) + "{NewLine}"// 模板
|
||
|
)
|
||
|
.CreateLogger();
|
||
|
try
|
||
|
{
|
||
|
// 创建Host
|
||
|
CreateHostBuilder(args).Build().Run();
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
Log.Fatal(ex, "Host terminated unexpectedly");
|
||
|
}
|
||
|
finally
|
||
|
{
|
||
|
Log.CloseAndFlush();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// 启用
|
||
|
/// </summary>
|
||
|
/// <param name="args"></param>
|
||
|
/// <returns></returns>
|
||
|
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||
|
Host.CreateDefaultBuilder(args)
|
||
|
.UseSerilog() // 使用Serilog作为日志提供者
|
||
|
.ConfigureWebHostDefaults(webBuilder =>
|
||
|
{
|
||
|
webBuilder.UseStartup<Startup>();
|
||
|
});
|
||
|
|
||
|
}
|
||
|
}
|