serilog / serilog-sinks-browserconsole

A console sink for the Blazor/Wasm environment
Apache License 2.0
62 stars 12 forks source link

JSRuntime.InvokeAsync issue #26

Open mouldt opened 9 months ago

mouldt commented 9 months ago

Firstly, excellent work that we have this sink.

Kindof related to https://github.com/serilog/serilog-sinks-browserconsole/issues/20 Create a New .Net8 WASM app (hosted) Add a simple javascript file to client wwwroot folder

In program.cs create WebAssemblyHostBuilder Add Serilog BrowserConsole Log something Now build the WebAssemblyHostBuilder Get the JSRuntime from DI container try to import a module using JSRuntime - module is returned as null

The expected behaviour is the module is returned as an IJSObjectReference

If I don't use the Logger before getting the module - it works A workaround is to put the module import in a loop, this works and it succeeds on the second try.

Another workaround as suggested in issue #20 is to pass the JSRuntime to the BrowserConsole sink. However, I do many things prior to building the DI container(initialising dynamically loaded modules etc), so I need to be able to log before building the WebAssemblyHostBuilder. This workaround is not an option for me and this scenario needs to be considered for issue #20.

The behaviour is the same using dotnet run and dotnet watch. So I don't think this is a tooling issue.

So while I have a workaround, there is clearly something here that is changing the default behaviour of JSRuntime.

Example repo is here

nblumhardt commented 9 months ago

Thanks for taking the time to write this up 👍

I don't personally have the bandwidth to be able to investigate fully just now, but I'm keen to see this fixed if possible and can review PRs/unblock things if someone else is able to jump in :-)