Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breaks a WASM .NET 7 app #20

Open
jrcpereira opened this issue Nov 9, 2022 · 7 comments
Open

Breaks a WASM .NET 7 app #20

jrcpereira opened this issue Nov 9, 2022 · 7 comments
Labels
good first issue Good for newcomers

Comments

@jrcpereira
Copy link

jrcpereira commented Nov 9, 2022

If we initialize the app with this sink, the app throws a null reference exception on the initialization of the app, causing it to fail to start.

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.AspNetCore.Components.WebAssembly.HotReload.WebAssemblyHotReload.InitializeAsync() at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHost.RunAsyncCore(CancellationToken cancellationToken, WebAssemblyCultureProvider cultureProvider) at Program.<Main>$(String[] args) in C:\xxxxxxxxxxx\Program.cs:line 147

@nblumhardt
Copy link
Member

Hi! I had a quick shot at reproducing this - looks a little like a Blazor hot reload bug, at first glance, since the app works fine with dotnet run and fails outside Serilog code in dotnet watch. The Blazor/ASP.NET Core components definitely shouldn't crash this way, even if there's something incompatible going on in the sink.

I've opened #21 to update the tooling and target versions of this project anyway. If someone has time to dig in and debug further, it'd be great to narrow this down enough to either mitigate here, or post a bug report/repro to the Blazor folks.

@cherry3
Copy link

cherry3 commented Nov 10, 2022

I had the same problem after updating to .NET 7, but worked around it by removing any call to Log.Info/Error/etc from Client Program.cs

@wtorricos
Copy link

wtorricos commented Nov 29, 2022

Are there any updates on this issue?

@nblumhardt
Copy link
Member

Hi! This needs someone with Blazor knowledge (and time) to investigate. It's most likely a bug in Blazor hot reloading (production code would most likely never be expected to throw NullReferenceException) so someone raising the issue with that team and posting a link to it here would also be a useful contribution. Thanks!

@wtorricos
Copy link

Hey guys, they asked me to provide a minimal repro project.
I tried to create a new project and reproduce the issue but with no luck. Does any of you have a project that we can share with the Blazor team?
If no one does I'll try to reproduce it during the weekend, but it will speed things if someone does.

@wtorricos
Copy link

wtorricos commented Dec 14, 2022

@here please take a look at the issue that I created, the guys from Blazor diagnosed it and found that it is actually a browser console issue dotnet/aspnetcore#45536
And they also left a workaround that it's working for me :)
Basically you need to do something like this;

WebAssemblyHost app = builder.Build();

Log.Logger = new LoggerConfiguration()
            .WriteTo.BrowserConsole(
                restrictedToMinimumLevel: LogEventLevel.Information,
                outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}",
                CultureInfo.InvariantCulture,
                jsRuntime: app.Services.GetRequiredService<IJSRuntime>())
            .CreateLogger();

await app.RunAsync();

The workaround is to pass the jsRuntime jsRuntime: app.Services.GetRequiredService<IJSRuntime>() parameter from the web host app.

@nblumhardt nblumhardt added the good first issue Good for newcomers label Dec 15, 2022
@nblumhardt
Copy link
Member

Awesome! Thanks for following that up!

I'm not sure about the conclusions regarding internal state - Serilog.Sinks.BrowserConsole only uses public APIs as far as I can tell (?) - but seems like the way forward will be to require an injected IJSRuntime either way 👍

Should be fixable with a new WriteTo.BrowserConsole overload, and deprecation of the old one. Would be great if anyone's able to take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants