Skip to content

Instantly share code, notes, and snippets.

@lambdageek
Last active October 13, 2023 14:00
Show Gist options
  • Save lambdageek/2d060ccc67810abdd1e51d780eeaa56f to your computer and use it in GitHub Desktop.
Save lambdageek/2d060ccc67810abdd1e51d780eeaa56f to your computer and use it in GitHub Desktop.
.NET 8 Additional WebAssembly logging

In .NET 8, Blazor projects now have a way to pass additional configuration options to the runtime, including setting environment variables before Mono starts.

The way to do it depends on whether you're using a blazorwasm template, or the new blazor (unified Server/Client project)

Blazor WebAssembly

If you created a blazorwasm project from the default template, in wwwroot/index.html replace this:

<body>
    <div id="app">
      ...
  </div>

  <div id="blazor-error-ui">
    ...
  </div>
    <script src="_framework/blazor.webassembly.js"></script>
</body>

with this

<body>
    <div id="app">
        ...
    </div>

    <div id="blazor-error-ui">
        ...
    </div>
    <script src="_framework/blazor.webassembly.js" autostart="false"></script>

    <script>
        Blazor.start({
            configureRuntime: dotnet => {
                console.log("configureRuntime");
                dotnet.withEnvironmentVariable("MONO_LOG_LEVEL", "debug");
                dotnet.withEnvironmentVariable("MONO_LOG_MASK", "all");
            }
        });
    </script></body>

Blazor

If you created a blazor project from the default template with Auto or WebAssembly interactivitiy, in the server project in App.razor replace this:

<body>
    <Routes />
    <script src="_framework/blazor.web.js"></script>
</body>

with this

<body>
    <Routes />
    <script src="_framework/blazor.web.js" autostart="false"></script>
    <script>
        Blazor.start({
            webAssembly: {
                configureRuntime: dotnet => {
                    console.log("in configureRuntime");
                    dotnet.withEnvironmentVariable("MONO_LOG_LEVEL", "debug");
                    dotnet.withEnvironmentVariable("MONO_LOG_MASK", "all");
                }
            }
        });
    </script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment