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

相关推荐
数据的世界014 小时前
技术变革:为何C#与.NET是未来的开发方向
java·c#·.net
大龄Python青年5 小时前
C#快入教程:Linux安装.NET
linux·c#·.net
向上的车轮5 小时前
Actix Web适合什么类型的Web应用?可以部署 Java 或 .NET 的应用程序?
java·前端·rust·.net
我是唐青枫6 小时前
C#.NET Random 深入解析:随机数生成原理与最佳实践
c#·.net
咕白m6257 小时前
如何通过 C# 提取 PDF 图片?单页与全文档提取
c#·.net
时光追逐者11 小时前
【拾遗补漏】.NET 常见术语集
微软·c#·.net
用户2986985301414 小时前
C#: 高效移动与删除Excel工作表
后端·.net·excel
小码编匠15 小时前
WPF 绘制图表合集-LiveCharts
后端·c#·.net
唐青枫17 小时前
C#.NET MemoryCache 深入解析:本地缓存机制与最佳实践
c#·.net