Sou*_*rer 5 logging blazor-client-side blazor-webassembly
我有一个直接从模板创建的 Blazor WebAssembly 应用程序,并且我添加了日志记录过程,如Blazor WebAssembly 日志记录中所述
我在我的类 Program 方法 Main 中添加了行 builder.Logging.SetMinimumLevel
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);
Run Code Online (Sandbox Code Playgroud)
注意:我没有添加特定的记录器,因为导航器控制台日志对我来说就足够了。
然后,我在我的剃刀组件中添加了日志记录
@using Microsoft.AspNetCore.SignalR.Client
@using GoodSales.Services
@inject NavigationManager NavigationManager
@using Microsoft.Extensions.Logging;
@inject ILogger<NavMenu> logger;
@inject ILoggerProvider LoggerProvider
Run Code Online (Sandbox Code Playgroud)
并在初始化方法中添加了测试线
protected override async Task OnInitializedAsync()
{
logger.LogDebug("LogDebug test");
logger.LogInformation("LogInformation test");
logger.LogWarning("LogWarning test");
Run Code Online (Sandbox Code Playgroud)
但是,在导航器控制台中,我只看到 LogInformation 和 LogWarning,但看不到 LogDebug。
我缺少什么?
You can try to use this 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.
Run Code Online (Sandbox Code Playgroud)
注意:在 .NET 5 Blazor Web Assembly 应用程序(不在服务器端)中,如果您使用标准日志记录,它将自动记录到浏览器控制台。但是,您的浏览器可能会过滤掉一些日志。这意味着如果您没有启用“详细/详细”日志记录,可能不会看到Debug日志Trace。检查您的设置。
上述所有内容仅适用于 Blazor Web Assembly(客户端)应用程序。如果您想从 Blazor 服务器托管应用程序登录到浏览器控制台,则只能使用第 3 方工具。使用这个 Nuget 包将发挥“魔力”。它的工作原理是通过 SignalR 通道将日志从服务器发送到用户的浏览器并登录到控制台。由于它需要一些复杂的设置,建议遵循此详细文档。