.net6使用Sejil可视化日志

(关注博主后,在"粉丝专栏",可免费阅读此文)

之前介绍了这篇.net 5使用LogDashboard_.net 5logdashboard rootpath-CSDN博客

这篇文章将会更加的简单,最终的效果都是可视化日志。

在程序非常庞大的时候,日志的作用就尤其的重要,日志能快速的定位程序的问题,从而高效率的解决问题。本文介绍使用Sejil来查询可视化日志。

这是Sejil的介绍https://github.com/alaatm/Sejil

1.创建一个.net6程序,安装Sejil

2.在Program.cs中增加

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.在控制器中增加日志

cs 复制代码
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

本文来源:

.net6使用Sejil可视化日志-CSDN博客

相关推荐
geovindu12 小时前
CSharp: Iterative Algorithms
开发语言·后端·算法·c#·.net·迭代算法
geovindu3 天前
CSharp: Flyweight Pattern
开发语言·后端·c#·.net·享元模式·结构型模式
驰骋工作流3 天前
请假流程:六款.NET工作流引擎实现方式对比
.net·ccflow·工作流引擎二开·.net工作流引擎对比
WarPigs3 天前
.NET项目app.Config笔记
c#·.net
海盗12344 天前
微软技术周报——2026-07-22
microsoft·c#·.net
海盗12344 天前
微软技术日报 ——2026-07-21
microsoft·c#·.net
半亩码田4 天前
【.NET新特性·第8篇】.NET 9 AI 构建基块:Microsoft.Extensions.AI
人工智能·microsoft·.net
Ctrl+Z侠4 天前
.NET WebApi Windows / Linux Docker 全链路压测与瓶颈定位 0 到 1 教程
linux·windows·.net
慧都小妮子4 天前
Aspose.CAD for .NET 26.6 更新发布
pdf·.net·3d渲染·cad·pdf导出·ifc编辑
夜莺悠吟5 天前
关于对 C# 中 ImplicitUsings,GlobalUsings 的讨论
c#·.net