Serilog + LogDashboard 搭建网页日志面板

Net中很多日志框架都是记录在文件或者数据库中。很不方面查看。这篇博客主要描述日志在线查看,直接在网站Url中查看。技术上主要用到Serilog+LogDashboard包。

安装NetGet包

Install-Package Serilog.AspNetCore
Install-Package LogDashboard

初始化

Program.cs

using LogDashboard;
using LogDashboard.Authorization.Filters;
using Serilog;
using Serilog.Events;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();

//面板不启用认证
builder.Services.AddLogDashboard(options => options.AddAuthorizationFilter(new CustomAuthorizeFilter()));

//面板启用认证
//builder.Services.AddLogDashboard(opt => opt.AddAuthorizationFilter(new LogDashboardBasicAuthFilter("admin", "123456")));

//Serilog配置
builder.Host.UseSerilog((hostingContext, loggerConfiguration) =>
{
	string logFilePath = Path.Combine(AppContext.BaseDirectory, "00_Log", ".log");

    //Log模板不可更改
	var outputTemplate = "{Timestamp:HH:mm} || {Level} || {SourceContext:l} || {Message} || {Exception} ||end {NewLine}";

	loggerConfiguration
	.Enrich.FromLogContext()
	.WriteTo.Console()
	.MinimumLevel.Debug()
	.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
	.WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day, outputTemplate: outputTemplate);
});


var app = builder.Build();

// Configure the HTTP request pipeline.

app.UseAuthorization();

//启用面板
app.UseLogDashboard();

//自定义Url
//app.UseLogDashboard("/LogDashboard");

app.MapControllers();


app.Run();




public class CustomAuthorizeFilter : LogDashboard.Authorization.ILogDashboardAuthorizationFilter
{
    public bool Authorization(LogDashboardContext context) => true;
}

查看日志

日志地址:Host(网站地址)+ LogDashboard。比如:http://localhost:5000/LogDashboard
Serilog如何写日志请查看相关文档中第一个链接。这里不多描述。

日志详情:

相关地址

Serilog相关文档

LogDashboard官网文档地址

LogDashboard-GitHub地址

posted @ 2022-11-04 01:01  雨水的命运  阅读(802)  评论(0编辑  收藏  举报