Visual log component: LogDashboard
preface
You don’t need to install a third party process, you just need to install the corresponding Nuget package in the project, add a few lines of code, you can achieve a log management panel with Web pages, very nice oh.
Here is the official introduction:
Official document address: doc.logdashboard.net/
❝
LogDashboard is an open source AspNetcore project on Github. It is designed to help developers quickly view logs to troubleshoot problems when a project is running
Usually, we will use nlog, log4net and other log components in the project. They are very powerful and complete for recording logs. In common cases, logs will be written to TXT or database, but it is not easy to view logs through Notepad and SQL. LogDashboard provides a simple and quick panel to view logs.
LogDashboard is applicable to aspNetcore 2. x-AspNetcore 3.x project and is developed using ASPNetcore middleware technology. Lightweight fast
OK, this article takes you through creating a new ASP.NET Core Web API project from 0, then adding the logging component Serilog, and finally completing the project with LogDashboard.
I believe that using LogDashboard can greatly improve the troubleshooting speed of your daily work.
Steps:
-
Create an ASP.NET Core Web API project
-
Add the Serilog log component
-
Add LogDashboard
-
Visual Log presentation
The actual practice of this article begins
1. Create an ASP.NET Core Web API project
This step is simple. Using VS 2019, create an ASP.NET Core Web API project called LogDashboardDemo.
2. Add the Serilog log component
2.1 Nuget Install Serilog package
Install-Package Serilog.AspNetCore
Copy the code
2.2 Add Serilog configuration in program. cs
public class Program { public static void Main(string[] args) { string logOutputTemplate = "{Timestamp:HH:mm:ss.fff zzz} || {Level} || {SourceContext:l} || {Message} || {Exception} ||end {NewLine}"; Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Default", LogEventLevel.Information) .MinimumLevel.Override("Microsoft", LogEventLevel.Error) .MinimumLevel.Override("Microsoft.Hosting.Lifetime", LogEventLevel.Information) .Enrich.FromLogContext() .WriteTo.Console(theme: Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme.Code) .WriteTo.File($"{AppContext.BaseDirectory}Logs/Dotnet9.log", rollingInterval: RollingInterval.Day, outputTemplate: logOutputTemplate) .CreateLogger(); CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .UseSerilog() .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); }Copy the code
Pay attention to code the format of the output log, log delimiter used “| |”, this is advice LogDashboard components, of course, you can modify, see LogDashboard detailed configuration document.
2.3 Verifying the Log Component Installation
Add the test log to startup. cs
public void ConfigureServices(IServiceCollection services) { Log.Information("ConfigureServices"); Log.Error(" Test Serilog to add exception Log "); Log.Fatal(" Test Serilog to add serious logs "); / /... }Copy the code
Operating Projects:
The log file \LogDashboardDemo\bin\Debug\net6.0\Logs\ dotnet920210417.log is generated in the output directory
08:37:27. 884 + 08:00 | | Information | | | | ConfigureServices | | | | end 08:37:27. 964 + 08:00 | | Error | | | | test Serilog add exception log | | | | end 08:37:27. 965 + 08:00 | | Fatal | | | | test Serilog add serious logs | | | | end 08:37:28. 154 + 08:00 | | Information | | | | the Configure | | | | end 08:37:28. 423 + 08:00 | | Information | | Microsoft. The Hosting. The Lifetime | | Now listening on: "Http://localhost:5000" | | | | end 08:37:28. 427 + 08:00 | | Information | | Microsoft. The Hosting. The Lifetime | | Application Started. Press Ctrl + C to shut down. | | | | end 08:37:28. 427 + 08:00 | | Information | | Microsoft. The Hosting. The Lifetime | | Hosting environment: "Development" | | | | end 08:37:28. 428 + 08:00 | | Information | | Microsoft. The Hosting. The Lifetime | | the Content root path: "C:\Users\Administrator\Desktop\LogDashboardDemo" || ||endCopy the code
Console output:
Console Log Output
Ok, the logging component has been added successfully, go to the next step.
3. Add LogDashboard
3.1 Nuget Install the LogDashboard package
Install-Package Serilog.AspNetCore
Copy the code
3.2 configuration LogDashboard
This step is simple, really simple, open startup. cs and add the following code:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogDashboard();
// ...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseLogDashboard();
// ...
}
Copy the code
That’s it. Using LogDashboard is as simple as that.
4. Visual log presentation
Run the project, the browser address bar: http://localhost:5000/logdashboard is log management panel, recorded directly under a small video demo: