Giter Site home page Giter Site logo

Comments (5)

nblumhardt avatar nblumhardt commented on May 26, 2024

At the time the code runs, JSRuntime.Current hasn't been initialized. We want to get in and set up logging early, since it means we can include exceptions generated during start-up in the log. Improvements welcome, though, if you spot any opportunities - thanks for checking it out!

from serilog-sinks-browserconsole.

Dresel avatar Dresel commented on May 26, 2024

Are multple MonoWebAssemblyJSRuntime within one Blazor application officially supported?

I'm asking because as I said this seems to interfere with other interop calls. If you modify your example with the following:

Reference index.js within index.html with the following content:

window.exampleJsFunctions = {
    showPrompt: function (text) {
        return prompt(text, 'Type your name here');
    }
};

Add this to Index.cshtml:

@functions
{
    protected override async Task OnInitAsync()
    {
        Console.WriteLine("OnInitAsync");

        string s = await JSRuntime.Current.InvokeAsync<string>(
            "exampleJsFunctions.showPrompt",
            "Name");

        Console.WriteLine(s);
    }
}

You won't get any return value from showPrompt 😞

But as soon as you comment out the two Log calls in Main.cs it works:

//Log.Debug("Hello, browser!");
//Log.Warning("Received strange response {@Response} from server", new { Username = "example", Cats = 7 });

from serilog-sinks-browserconsole.

Dresel avatar Dresel commented on May 26, 2024

What about making it possible (necessary) to inject the IJSRuntime? Then you could do something like this:

public static void Main(string[] args)
{
    SelfLog.Enable(m => Console.Error.WriteLine(m));

    var monoWebAssemblyJsRuntime = new MonoWebAssemblyJSRuntime();

    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .WriteTo.BrowserConsole(monoWebAssemblyJsRuntime)
        .CreateLogger();

    Log.Debug("Hello, browser!");
    Log.Warning("Received strange response {@Response} from server", new { Username = "example", Cats = 7 });

    try
    {
        CreateHostBuilder(args).ConfigureServices(x =>
        {
            x.AddSingleton<IJSRuntime>(monoWebAssemblyJsRuntime);
        }).Build().Run();
    }
    catch (Exception ex)
    {
        Log.Fatal(ex, "An exception occurred while creating the WASM host");
        throw;
    }
}

from serilog-sinks-browserconsole.

nblumhardt avatar nblumhardt commented on May 26, 2024

Interesting! I'm not knowledgeable enough to pick the right approach for Blazor here, might need some advice from @SteveSandersonMS 😊

from serilog-sinks-browserconsole.

nblumhardt avatar nblumhardt commented on May 26, 2024

#8 touches on this issue and has some details regarding the current state of Blazor, so closing this in favor of that one. Thanks!

from serilog-sinks-browserconsole.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.