.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博客

相关推荐
~plus~11 小时前
.NET 8 C# 委托与事件实战教程
网络·c#·.net·.net 8·委托与事件·c#进阶
rockey62715 小时前
AScript动态脚本多语言环境支持
sql·c#·.net·script·eval·function·动态脚本
dotNET实验室16 小时前
ASP.NET Core 内存缓存实战:一篇搞懂该怎么配、怎么避坑
.net
龙侠九重天16 小时前
ML.NET 实战:快速构建分类模型
分类·数据挖掘·c#·.net
无风听海18 小时前
.NET10之内置日志配置与使用指南
asp.net·.net
沃尔威武1 天前
数据库 Sinks(.net8)
数据库·.net·webview
大尚来也1 天前
告别“字符串拼接”:在.NET中用LINQ重塑数据查询
.net·solr·linq
无风听海2 天前
.NET10之Record 深度解析
.net
CodeCraft Studio2 天前
LightningChart .NET v12.5.1 发布:高性能数据可视化再升级,赋能工业与实时数据场景
信息可视化·.net·gpu·数据可视化·lightningchart·高性能图表开发·数据可视化引擎
CyL_Cly2 天前
.net framework 3.5下载( 2.0-4.8大全 )
.net