dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

[Question] [Server side rendering] Guidance on adding logging to Blazor app #15706

Closed MarkStega closed 4 years ago

MarkStega commented 6 years ago

I had tried SSR when 0.5 arrived and other than some dropping of the SignalR update path my application worked as it did on the client.

I just decided to try SSR again and the app fails. I suspect it is because the client is now filled (some say littered) with calls to Blazor.Extensions.Logging methods.

I thought (looking at the architecture diagram in the 0.5 announcement blog) that I could simply add the references to NLog as I do on the web server itself.

I started by removing this fragment

            services.AddLogging(builder => builder
                .AddBrowserConsole() // Add Blazor.Extensions.Logging.BrowserConsoleLogger
                .SetMinimumLevel(LogLevel.Trace)
            );

from ConfigureServices(IServiceCollection services)

I was immediately stymied when trying to add this fragment

               .ConfigureLogging(logging =>
                   {
                       logging.ClearProviders();
                       logging.SetMinimumLevel(LogLevel.Trace);
                   })
               .UseNLog()  // NLog: setup NLog for Dependency injection

to BlazorWebAssemblyHost.CreateDefaultBuilder just as I add it to WebHost.CreateDefaultBuilder in the web server program.cs

Is this an issue that the CreateDefaultBuilder has to have 'UseNLog()' added meaning that the NLog team (or I) need to write an extension to BlazorWebAssemblyHost.CreateDefaultBuilder?

SteveSandersonMS commented 6 years ago

Where do you want your log messages to show up?

If you think I'm misunderstanding the question please let me know.

SteveSandersonMS commented 6 years ago

Update: BrowserConsoleLogger doesn't support server-side execution yet, because it relies on IJSInProcessRuntime. I'd recommend this library be updated to use async interop rather than sync, as then it should just work.

MarkStega commented 6 years ago

@SteveSandersonMS

If you want them to be emitted to the server's log, then you don't need to do anything different from a normal server-side ASP.NET Core application. All the .NET is running on the server, so just configure your logging service in the server's DI Startup.cs, and inject and use ILogger wherever you want (including in Blazor components)

So obvious in retrospect and I am getting log messages as expected. The issue I raised was a 'red herring' and now that I have logging I see the issue ('@Inject HttpClient xyzzy' does not behave). I'll put it on gitter to start and if no joy will open an issue here.