Blazor WebAssembly Logging is not honoring SetMinimumLevel

1.3k views Asked by At

I have a Blazor WebAssembly application created directly from template, and I have added logging procedures as described in Blazor WebAssembly Logging

I have added the line builder.Logging.SetMinimumLevel in my class Program, method 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);

NOTICE: I have no added specific logger, because navigator console log is enough for me.

Then, I have added logging in my razor component

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

And added testing line in the initialization method

protected override async Task OnInitializedAsync()
  {

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

But, in the navigator console I only see the LogInformation and the LogWarning, but not the LogDebug.

What am I missing?

1

There are 1 answers

4
Major On BEST ANSWER

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.

NOTE: in .NET 5 Blazor Web Assembly apps (NOT in Server side) if you use standard logging it will log to Browser console automatically. However it is possible your Browser is filtering out some logs. It means if you are not enabling "Verbose/Detailed" logging probably won't see Debug and Trace logs. Check your settings.

In Chrome it is here: enter image description here

All above mentioned works only for Blazor Web Assembly (client side) Apps. If you want to log from a Blazor Server hosted app to your Browser console, then it is only possible with 3rd party tool. Use 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.