溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET Core中怎么使用可視化日志組件

發(fā)布時間:2021-06-22 17:16:48 來源:億速云 閱讀:215 作者:Leah 欄目:編程語言

ASP.NET Core中怎么使用可視化日志組件,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

ASP.NET Core中怎么使用可視化日志組件

前言

今天站長推薦一款日志可視化組件LogDashboard,可以不用安裝第三方進程,只需要在項目中安裝相應(yīng)的Nuget包,添加數(shù)行代碼,就可以實現(xiàn)擁有帶Web頁面的日志管理面板,十分nice哦。

下面是官方介紹:

官方文檔地址:https://doc.logdashboard.net/

LogDashboard是在github上開源的aspnetcore項目, 它旨在幫助開發(fā)人員排查項目運行中出現(xiàn)錯誤時快速查看日志排查問題

通常我們會在項目中使用nlog、log4net等日志組件,它們用于記錄日志的功能非常強大和完整,常見情況會將日志寫到txt或數(shù)據(jù)庫中, 但通過記事本和sql查看日志并不簡單方便. LogDashboard提供了一個可以簡單快速查看日志的面板.

LogDashboard適用于aspnetcore 2.x - aspnetcore3.x 項目, 采用aspnetcore中間件技術(shù)開發(fā). 輕量快速

OK,本文帶大家從0創(chuàng)建一個ASP.NET Core Web API新項目,然后添加日志組件Serilog,最后搭配使用LogDashboard完成此項目。

相信使用LogDashboard能極大提高你平時工作中的問題排查速度。

步驟:

  1. 創(chuàng)建一個ASP.NET Core Web API項目

  2. 添加Serilog日志組件

  3. 添加LogDashboard

  4. 可視化日志演示


本文實戰(zhàn)開始

1. 創(chuàng)建一個ASP.NET Core Web API項目

這一步很簡單,使用VS 2019,創(chuàng)建一個ASP.NET Core Web API項目,命名為LogDashboardDemo。

2. 添加 Serilog 日志組件

2.1 Nuget 安裝 Serilog 包

Install-Package Serilog.AspNetCore

2.2 Program.cs 中添加 Serilog 配置

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

注意代碼中輸出日志的格式,日志分隔符使用 "||",這是LogDashboard組件的建議,當(dāng)然你可以修改,詳細配置見LogDashboard文檔。

2.3 驗證日志組件安裝成功

Startup.cs中添加測試日志

public void ConfigureServices(IServiceCollection services)
{
  Log.Information("ConfigureServices");
  Log.Error("測試Serilog添加異常日志");
  Log.Fatal("測試Serilog添加嚴重日志");
  // ....
}

運行項目:

輸出目錄下產(chǎn)生日志文件:\LogDashboardDemo\bin\Debug\net6.0\Logs\Dotnet920210417.log

08:37:27.884 +08:00 || Information ||  || ConfigureServices ||  ||end 
08:37:27.964 +08:00 || Error ||  || 測試Serilog添加異常日志 ||  ||end 
08:37:27.965 +08:00 || Fatal ||  || 測試Serilog添加嚴重日志 ||  ||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

控制臺輸出:

ASP.NET Core中怎么使用可視化日志組件

好了,日志組件已經(jīng)添加成功,進入下一步。

3. 添加 LogDashboard

3.1 Nuget安裝 LogDashboard 包

Install-Package Serilog.AspNetCore

3.2 配置 LogDashboard

這一步很簡單,真的很簡單,打開Startup.cs,添加如下代碼:

public void ConfigureServices(IServiceCollection services)
{
  services.AddLogDashboard();
  // ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
  app.UseLogDashboard();
  // ...
}

關(guān)于ASP.NET Core中怎么使用可視化日志組件問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI