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