Blazor WebAssembly 日志记录不遵守 SetMinimumLevel

Blazor WebAssembly Logging is not honoring SetMinimumLevel

我有一个直接从模板创建的 Blazor WebAssembly 应用程序,并且我添加了 Blazor WebAssembly Logging

中所述的日志记录过程

我在我的 class 程序中添加了行 builder.Logging.SetMinimumLevel,方法 Main

public class Program
{
    const string serverHttpClientName = "GoodSales.ServerAccessApi";

    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
        IConfiguration configuration = builder.Configuration;

        // JMA: Configure logging
        builder.Logging.SetMinimumLevel(LogLevel.Debug);

注意:我没有添加特定的记录器,因为导航器控制台日志对我来说已经足够了。

然后,我在我的剃须刀组件中添加了日志记录

@using Microsoft.AspNetCore.SignalR.Client
@using GoodSales.Services
@inject NavigationManager NavigationManager
@using Microsoft.Extensions.Logging;
@inject ILogger<NavMenu> logger;
@inject ILoggerProvider LoggerProvider

并在初始化方法中添加了测试线

protected override async Task OnInitializedAsync()
  {

      logger.LogDebug("LogDebug test");
      logger.LogInformation("LogInformation test");
      logger.LogWarning("LogWarning test");

但是,在导航器控制台中我只看到 LogInformation 和 LogWarning,但看不到 LogDebug。

我错过了什么?

你可以试试这个Nuget package In my project it logs Debug messages as well when correctly configured. It works always because using Console.WriteLine() See docs

using Blazor.WebAssembly.Logging.Console;

...

builder.Logging.AddBrowserConsole()
    .SetMinimumLevel(LogLevel.Debug) //Setting LogLevel is optional
    .AddFilter("Microsoft", LogLevel.Information); //System logs can be filtered.

注意:在 .NET 5 Blazor Web Assembly 应用程序(不在服务器端)中,如果您使用标准日志记录,它将自动记录到浏览器控制台。但是 您的浏览器可能会过滤掉一些日志。这意味着如果您不启用“Verbose/Detailed”日志记录可能不会看到 DebugTrace 日志。检查您的设置。

Chrome中它在这里:

上述所有内容仅适用于 Blazor Web Assembly(客户端)应用程序。如果您想 从 Blazor Server 托管应用程序登录到您的浏览器控制台 ,则只能使用第 3 方工具。使用 this Nuget package which will do the "magic". It works by sending logs from your server via SignalR channel to the user's Browser and logging to the console. Since it requires somewhat complex setup recommend to follow this detailed docs.