开源日志组件Sejil--附带日志管理界面
1.开源日志组件源码: https://github.com/alaatm/Sejil
2.下载下来发现里面对于不同的.net core 版本的配置提供了对应的示例
.Net Core 3.1
Program.cs文件中
public class Program
{
public static void Main(string[] args) => CreateHostBuilder(args).Build().Run();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSejil()
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sejil;
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) => Configuration = configuration;
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.ConfigureSejil(cfg => cfg.Title = "Logs");
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSejil();
app.UseRouting();
app.UseEndpoints(endpoints =>
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"));
}
}
如果需要权限校验之类的
Program.cs文件中
public class Program
{
public static void Main(string[] args)
{
AppContext.SetSwitch("System.Net.Http.UseSocketsHttpHandler", false);
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSejil()
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
}
Startup.cs 文件中
using Bazinga.AspNetCore.Authentication.Basic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Sejil;
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration) => Configuration = configuration;
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddAuthentication(BasicAuthenticationDefaults.AuthenticationScheme)
.AddBasicAuthentication(credentials =>
Task.FromResult(credentials.username == "myUsername" && 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, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSejil();
app.UseEndpoints(endpoints =>
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"));
}
}
对应的 appsettings.json 配置文件
"Authentication": {
"Authority": "https://sample.auth0.com",
"ResponseType": "code",
"ClientId": "USER_SECRET",
"ClientSecret": "USER_SECRET",
"Scope": [
"openid",
"profile",
"email"
],
"CallbackPath": "/signin-auth0",
"ClaimsIssuer": "Auth0"
}
.Net Core 6
using Sejil;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
// Configure Sejil
builder.Host.UseSejil(
minLogLevel: LogLevel.Information,
writeToProviders: true);
builder.Services.ConfigureSejil(cfg => cfg.Title = "My App Logs");
//////////////////
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
// Add sejil to request pipeline
app.UseSejil();
//////////////////
app.Run();
另外补充一下: 源代码里面是 在本地 C:\Users\{电脑名}\AppData\Local\ 项目 下新建了 一个sqlite 文件
如果是项目发布,是在运行程序目录下 新建一个 业务文件夹\sqlite文件
访问 http://localhost:xxxx/Sejil 就可以看到对应的日志了.
本文来自博客园,作者:兴想事成,转载请注明原文链接:https://www.cnblogs.com/mjxxsc/p/17625168.html