(关注博主后,在“粉丝专栏”,可免费阅读此文)?? ?? ? ?
? ? ? ? 之前介绍了这篇.net 5使用LogDashboard_.net 5logdashboard rootpath-CSDN博客
?这篇文章将会更加的简单,最终的效果都是可视化日志。
? ? ? ? 在程序非常庞大的时候,日志的作用就尤其的重要,日志能快速的定位程序的问题,从而高效率的解决问题。本文介绍使用Sejil来查询可视化日志。
这是Sejil的介绍https://github.com/alaatm/Sejil?
1.创建一个.net6程序,安装Sejil
2.在Program.cs中增加
using Microsoft.AspNetCore.Hosting;
using Sejil;
namespace WebApplication1
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Host.UseSejil(minLogLevel: LogLevel.Information, writeToProviders: true); //增加
builder.Services.ConfigureSejil(cfg => cfg.Title = "故里2130的日志");
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.UseSejil();//增加
app.Run();
}
}
}
其中LogLevel.Information是日志的等级
3.在控制器中增加日志
using Microsoft.AspNetCore.Mvc;
namespace WebApplication1.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogDebug("我是LogDebug");
_logger.LogInformation("我是LogInformation");
_logger.LogWarning("我是LogWarning");
_logger.LogError("我是LogError");
_logger.LogCritical("我是LogCritical");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
}
4.此时我们运行api程序
然后点击GET方法
5.在原api的IP地址后面增加Sejil即可
打开https://localhost:7057/Sejil
6.效果
可以根据类型进行筛选
本文源码
https://download.csdn.net/download/u012563853/88657944
本文来源: