集成 Serilog

前言

serilog是NET下优秀的日志项目,对NETCore支持友好,扩展丰富。abp框架更是将其做为框架默认的日志组件,logdashboard完全支持serilog

创建NETCore项目

如同快速入门一样,我们需要创建一个NETCore项目,不在赘述此过程

安装 Serilog

1
Install-Package Serilog.AspNetCore
Copied!
使用以下代码覆盖 Program
1
public static void Main(string[] args)
2
{
3
Log.Logger = new LoggerConfiguration()
4
.MinimumLevel.Debug()
5
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
6
.Enrich.FromLogContext()
7
.WriteTo.File(quot;{AppContext.BaseDirectory}Log/.log", rollingInterval:RollingInterval.Day,outputTemplate: "{Timestamp:HH:mm} || {Level} || {SourceContext:l} || {Message} || {Exception} ||end {NewLine}")
8
.CreateLogger();
9
10
CreateWebHostBuilder(args).Build().Run();
11
}
12
13
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
14
WebHost.CreateDefaultBuilder(args)
15
.UseSerilog()
16
.UseStartup<Startup>();
Copied!

安装LogDashboard

1
Install-Package LogDashboard
Copied!
打开Startup.cs
ConfigureServices方法中配置服务
1
public void ConfigureServices(IServiceCollection services)
2
{
3
services.AddLogDashboard();
4
}
Copied!
Configure方法中配置中间件
1
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
2
{
3
if (env.IsDevelopment())
4
{
5
app.UseDeveloperExceptionPage();
6
}
7
8
app.UseLogDashboard();
9
10
app.Run(async (context) =>
11
{
12
await context.Response.WriteAsync("Hello World!");
13
});
14
}
Copied!
运行项目导航到/logdashboard
enjoy
Last modified 2yr ago