Sejil 日志收集框架

一个小项目的简单日志记录框架 支持web查看
dotnet add package Sejil --version 3.0.4
或使用Nuget 可视化

将以下代码添加到Program.cs

public static IWebHost BuildWebHost(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseSejil()  // <-- Add this line  默认记录级别是Info
        .ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());            

将以下代码添加到Startup.cs

using Sejil;

public class Startup
{    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        // ...
        app.UseSejil();  // <-- Add this line
        // ...
    }
}

(可选)查看日志时需要身份验证:

    public void ConfigureServices(IServiceCollection services)
    {
        services.ConfigureSejil(options =>
        {
            options.AuthenticationScheme = /* Your authentication scheme */
        });
    }

使用 Serilog 的配置来指定某些名称空间的最低日志级别覆盖。例如,要将日志记录限制为Microsoft.AspNetCoreWarning

{
    "Serilog": {
        "MinimumLevel": {
            "Override": {
                "Microsoft.AspNetCore": "Warning"
            }
        }
    },
    "AllowedHosts": "*"
}    

用户名密码授权

  public void ConfigureServices(IServiceCollection services)
  {           
            //授权
            services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
                .AddBasicAuthentication(credentials =>
                {
                    return Task.FromResult(credentials.username == "myUsername1" && credentials.password == "myPassword");
                }
                );

            services.ConfigureSejil(cfg => cfg.AuthenticationScheme = BasicAuthenticationDefaults.AuthenticationScheme);
}

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
            //授权
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseSejil();
     
}

 

 

使用

 _logger.LogDebug("LogDebug测试");
 _logger.LogError(new NullReferenceException("cccccc"),"LogError测试");
_logger.LogInformation( "LogInformation测试");

 

查看日志:http://IP:端口/Sejil

 

 

参考:https://github.com/alaatm/Sejil

posted @ 2023-08-18 11:30  zhuweirandy  阅读(97)  评论(0编辑  收藏  举报