Skip to content

Blazor Hybrid (Blazor + MAUI) #30

@NovoDwarf

Description

@NovoDwarf

I attempted to integrate this into my project. The first issue I encountered was the requirement for .NET 8.0, so I created a fork and upgraded the project to .NET 9.0. The solution compiled successfully, and other sinks (such as File, Debug, and LiteDB) were functioning correctly. However, no output appeared in the Browser Console.

In contrast, the sample project from the repository worked as expected — the browser console output was functional.

In my project, logging was configured using ServiceCollectionExtensions like this:

services.AddSerilog((provider, lc) => lc
	.ReadFrom.Services(provider)
	.Enrich.FromLogContext()
	.WriteTo.BrowserConsole(jsRuntime: provider.GetRequiredService<IJSRuntime>())
	// other sinks
);

I also attempted to log using the static Log class, retrieving IJSRuntime via app.Services.GetRequiredService<IJSRuntime>() (after calling var app = builder.Build();). Despite these efforts, the browser console remained empty.

To investigate further, I enabled Serilog’s SelfLog and observed the following exception:

Cannot invoke JavaScript outside of a WebView context

After some research, I found that this error is related to the absence of a proper WebView context, which is initialized by calling builder.Services.AddMauiBlazorWebView(). I moved my logging configuration to occur after this call, which resolved the exception. However, logging to the browser console still did not work — neither via the static Log class nor through dependency injection.

I also tried calling builder.Logging.AddSerilog() and repeated with and without the existing logger, but it had no effect. In the project, logging on Razor Components was done via injecting ILogger. Right now, it's hard for me to know if my issue is related to the NuGet package itself or if I'm doing something wrong, just logging to the Browser Console is perfect for Blazor Hybrid applications, so I'd like to figure it out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions