question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Debug-level configuration not respected by logger in blazor wasm

See original GitHub issue

To reproduce

  1. Create a new Blazor WASM project.
$ dotnet new blazorwasm -o logger-repro 
  1. Create a wwwroot/appsettings.json file with the following contents:
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  }
}
  1. Create a wwwroot/appsettings.Development.json file with the following contents:
{
  "Logging": {
    "LogLevel": {
      "Default": "Trace"
    }
  }
}
  1. Add package reference to logger-repro.csproj:
<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
    <UseBlazorWebAssembly>true</UseBlazorWebAssembly>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0-preview.7.20365.19" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-preview.7.20365.19" PrivateAssets="all" />
    <PackageReference Include="System.Net.Http.Json" Version="5.0.0-preview.7.20364.11" />

    <PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="5.0.0-preview.7.20364.11" />
  </ItemGroup>

</Project>
  1. Setup logging in Program.cs by adding the following line:
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
  1. Add a set of logging statements to the Counter component.
@page "/counter"

@using Microsoft.Extensions.Logging
@inject ILogger<Counter> logger;

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    private void IncrementCount()
    {
        logger.Log(LogLevel.Critical, "This is critical.");
        logger.Log(LogLevel.Error, "This is error.");
        logger.Log(LogLevel.Warning, "This is warning.");
        logger.Log(LogLevel.Information, "This is info.");
        logger.Log(LogLevel.Debug, "This is debug.");
        logger.Log(LogLevel.Trace, "This is trace.");
        currentCount++;
    }
}
  1. Run the app and observe that when you click the increment button the critical, error, warning, and information log levels are displayed. Although the log level is set to trace the debug and trace messages are missing.

To ensure that the environment is correct and the correct log level is loaded in the configuration, I added this two lines to the Program.cs file:

Console.WriteLine(builder.HostEnvironment.Environment);
Console.WriteLine(builder.Configuration.GetValue<string>("Logging:LogLevel:Default"));

It also looks like the app is crashing… image

Environement:

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   5.0.100-preview.7.20366.6
 Commit:    0684df3a5b

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-preview.7.20366.6\

Host (useful for support):
  Version: 5.0.0-preview.7.20364.11
  Commit:  53976d38b1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
msschlcommented, Aug 25, 2020

Closing this issue since I found the cause of my problem. 🙈

For all having the same issue: Check the log level in the chrome browser console. The default setting only shows messages with a log level of info, warning and errors. image

0reactions
msftbot[bot]commented, Aug 11, 2020

We’ve moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor WebAssembly Logging is not honoring ...
You can try to use this Nuget package In my project it logs Debug messages as well when correctly configured. It works always...
Read more >
ASP.NET Core Blazor logging
This article explains Blazor app logging, including configuration and how to write log messages from Razor components.
Read more >
Blazor WebAssembly - Changing The Log Level At Runtime
Learn how to change the log level of your Blazor WebAssembly application at runtime for easier debugging.
Read more >
Don't let ASP.NET Core Console Logging Slow your App down
x Console logging is very, very slow when it is set to Information or worse Debug . The default ASP.NET Core default if...
Read more >
Relay Blazor client logs to Serilog in ASP.NET Core
Ingestion together implement a client/server log relay for Blazor apps running Serilog, feeding events from the Blazor client into the ASP.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found