90 lines
4.3 KiB
C#
90 lines
4.3 KiB
C#
using Furion;
|
||
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.Hosting;
|
||
using Microsoft.Extensions.Logging;
|
||
using Serilog;
|
||
using Serilog.Events;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Waste.Web.Entry
|
||
{
|
||
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.Inject()
|
||
.UseStartup<Startup>()
|
||
.UseSerilogDefault(config =>
|
||
{
|
||
var filepath = App.Configuration["logfile"];
|
||
string date = DateTime.Now.ToString("yyyy-MM-dd");//按时间创建文件夹
|
||
string outputTemplate = "{NewLine}【{Level:u3}】{Timestamp:yyyy-MM-dd HH:mm:ss.fff}" +
|
||
"{NewLine}#Msg#{Message:lj}" +
|
||
"{NewLine}#Pro #{Properties:j}" +
|
||
"{NewLine}#Exc#{Exception}" +
|
||
new string('-', 50);//输出模板
|
||
///1.输出所有restrictedToMinimumLevel:LogEventLevel类型
|
||
config
|
||
.WriteTo.Seq("http://localhost:5341/")
|
||
//.MinimumLevel.Debug() // 所有Sink的最小记录级别
|
||
//.MinimumLevel.Override("Microsoft", LogEventLevel.Fatal)
|
||
//.Enrich.FromLogContext()
|
||
.WriteTo.Console(outputTemplate: outputTemplate)
|
||
|
||
//2.1仅输出 LogEventLevel.Debug 类型
|
||
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Debug)//筛选过滤
|
||
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Debug}.log",
|
||
outputTemplate: outputTemplate,
|
||
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
||
encoding: Encoding.UTF8 // 文件字符编码
|
||
)
|
||
)
|
||
|
||
//2.2仅输出 LogEventLevel.Error 类型
|
||
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Error)//筛选过滤
|
||
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Error}.log",
|
||
outputTemplate: outputTemplate,
|
||
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
||
encoding: Encoding.UTF8 // 文件字符编码
|
||
)
|
||
)
|
||
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Information)//筛选过滤
|
||
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Information}.log",
|
||
outputTemplate: outputTemplate,
|
||
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
||
encoding: Encoding.UTF8 // 文件字符编码
|
||
)
|
||
)
|
||
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Fatal)//筛选过滤
|
||
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Fatal}.log",
|
||
outputTemplate: outputTemplate,
|
||
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
||
encoding: Encoding.UTF8 // 文件字符编码
|
||
)
|
||
)
|
||
.WriteTo.Logger(lg => lg.Filter.ByIncludingOnly(evt => evt.Level == LogEventLevel.Warning)//筛选过滤
|
||
.WriteTo.File($"{filepath}/{date}/{LogEventLevel.Warning}.log",
|
||
outputTemplate: outputTemplate,
|
||
rollingInterval: RollingInterval.Day,//日志按日保存,这样会在文件名称后自动加上日期后缀
|
||
encoding: Encoding.UTF8 // 文件字符编码
|
||
)
|
||
)
|
||
;
|
||
})
|
||
;
|
||
});
|
||
}
|
||
}
|