Using ASP.NET Core Visual Log Component

Using ASP.NET Core Visual Log Component

Today, the site owner recommends a log visualization component `LogDashboard`. It does not require installing third-party processes. Just install the corresponding **NuGet** package in your project and add a few lines of code to have a web-based log management panel. It's really nice.

Last updated 4/17/2021 8:41 PM
沙漠尽头的狼
5 min read
Category
ASP.NET Core
Tags
.NET C# ASP.NET Core UI Design NuGet

Visual Log Component: LogDashboard

Visual Log Component: LogDashboard

Introduction

Today, the site owner recommends a visual log component, LogDashboard. It eliminates the need for installing third‑party processes – just add the relevant NuGet package to your project and write a few lines of code to have a web‑based log management panel. Very nice indeed.

Here is the official description:

Official docs: https://doc.logdashboard.net/

LogDashboard is an open‑source ASP.NET Core project on GitHub. It aims to help developers quickly view logs when errors occur during project execution to facilitate troubleshooting.

We often use log components like NLog and log4net in our projects. They are powerful and complete for logging, but typically logs are written to text files or databases. Viewing logs via Notepad or SQL is not convenient. LogDashboard provides a panel for quickly checking logs.

LogDashboard works with ASP.NET Core 2.x – 3.x projects, built using ASP.NET Core middleware technology. It’s lightweight and fast.

OK, this article will guide you through creating a new ASP.NET Core Web API project from scratch, adding the Serilog logging component, and finally integrating LogDashboard to complete the project.

I believe using LogDashboard will significantly improve your ability to troubleshoot issues during daily work.

Steps:

  1. Create an ASP.NET Core Web API project
  2. Add the Serilog logging component
  3. Add LogDashboard
  4. Visual log demonstration

Let’s start the hands‑on part

1. Create an ASP.NET Core Web API project

This step is straightforward. Using VS 2019, create an ASP.NET Core Web API project named LogDashboardDemo.

2. Add the Serilog logging component

2.1 Install the Serilog NuGet package

Install-Package Serilog.AspNetCore

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>();
      });
}

Note the log output format: the log separator uses "||". This is recommended by the LogDashboard component. You can modify it if needed – see the LogDashboard documentation for details.

2.3 Verify the log component is installed successfully

Add test log entries in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
  Log.Information("ConfigureServices");
  Log.Error("Test Serilog exception log");
  Log.Fatal("Test Serilog fatal log");
  // ....
}

Run the project:

A log file is generated in the output directory: \LogDashboardDemo\bin\Debug\net6.0\Logs\Dotnet920210417.log

08:37:27.884 +08:00 || Information ||  || ConfigureServices ||  ||end
08:37:27.964 +08:00 || Error ||  || Test Serilog exception log ||  ||end
08:37:27.965 +08:00 || Fatal ||  || Test Serilog fatal log ||  ||end
08:37:28.154 +08:00 || Information ||  || Configure ||  ||end
08:37:28.423 +08:00 || Information || Microsoft.Hosting.Lifetime || Now listening on: "http://localhost:5000" ||  ||end
08:37:28.427 +08:00 || Information || Microsoft.Hosting.Lifetime || Application started. Press Ctrl+C to shut down. ||  ||end
08:37:28.427 +08:00 || Information || Microsoft.Hosting.Lifetime || Hosting environment: "Development" ||  ||end
08:37:28.428 +08:00 || Information || Microsoft.Hosting.Lifetime || Content root path: "C:\Users\Administrator\Desktop\LogDashboardDemo" ||  ||end

Console output:

Console log output

Great, the log component is ready. Let’s move to the next step.

3. Add LogDashboard

3.1 Install the LogDashboard NuGet package

Install-Package LogDashboard

3.2 Configure LogDashboard

This step is very simple – truly 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();
  // ...
}

That’s it – using LogDashboard is that easy.

4. Visual log demonstration

Run the project, and enter http://localhost:5000/logdashboard in your browser to open the log management panel. Here’s a short video demonstration:

Keep Exploring

Related Reading

More Articles