logdashboard是在github上开源的aspnetcore项目, 它旨在帮助开发人员排查项目运行中出现错误时快速查看日志排查问题git
一般咱们会在项目中使用nlog、log4net等日志组件,它们用于记录日志的功能很是强大和完整,常见状况会将日志写到txt
或数据库
中, 但经过记事本和sql查看日志并不简单方便. LogDashboard提供了一个能够简单快速查看日志的面板.github
LogDashboard适用于aspnetcore 2.x - aspnetcore3.x 项目, 采用aspnetcore中间件
技术开发. 轻量快速。web
实时查看应用程序运行中产生的日志sql
复合检索全部日志并查看详情等操做数据库
本文示例源码在 https://github.com/liangshiw/LogDashboard/tree/master/samples/DotNetCoreEmptyUseNlog浏览器
确保机器上安装了NetCore SDK,打开PowerShell运行如下命令,咱们将建立一个AspNetCore空项目app
dotnet new empty
使用VSCode或VisualStudio打开项目,这时咱们还须要作一些其余的准备工做。日志组件选用Nlogasync
Install-Package NLog.Web.AspNetCore
打开Program.cs
在CreateHostBuilder
方法中添加Nlog中间件,复制如下代码覆盖CreateHostBuilder
方法ide
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder=> { webBuilder.UseStartup<Startup>() .ConfigureLogging(logging => { logging.ClearProviders(); logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Information); }) .UseNLog(); });
添加一个Nlog.config到项目中,并右键文件设置为复制到输出目录,如下是Nlog.config的所有内容ui
配置文件须要分隔符才能够被LogDashboard解析,默认是||与||end,也能够自定义,请参阅LogDashboard配置
<?xml version="1.0" encoding="utf-8" ?> <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <variable name="myvar" value="myvalue"/> <targets> <target xsi:type="file" name="File" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate}||${level}||${logger}||${message}||${exception:format=ToString:innerFormat=ToString:maxInnerExceptionLevel=10:separator=\r\n}||end" /> </targets> <rules> <logger name="*" minlevel="Debug" writeTo="file" /> </rules> </nlog>
准备工做已经结束,这时安装 LogDashboard
Install-Package LogDashboard
打开Startup.cs咱们要作两件事
ConfigureServices
方法中配置服务public void ConfigureServices(IServiceCollection services) { services.AddLogDashboard(); }
关于更多的配置请参阅 LogDashboard配置
Configure
方法中配置中间件public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseLogDashboard(); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); }
运行项目,在浏览器中导航到 /logdashboard
enjoy