serilog-sinks-browserhttp icon indicating copy to clipboard operation
serilog-sinks-browserhttp copied to clipboard

The usage in a blazor RC1 project breaks the NavigationManager

Open alexfdezsauco opened this issue 4 years ago • 6 comments

The usage in a blazor RC1 project breaks the NavigationManager.

How to reproduce:

  1. Assume that you have a service locator that resolve backed services. In this case the ingest backend.

Program.cs

    ....
    host = await host.RecreateLogAsync();
    try
    {
        await host.RunAsync();
    }
    catch (Exception ex)
    {
        Log.Fatal(ex, "An exception occurred while creating the web assembly host");
        throw;
    }

WebAssemblyHostExtensions.cs

public static class WebAssemblyHostExtensions
{
    public static async Task<WebAssemblyHost> RecreateLogAsync(this WebAssemblyHost @this)
    {
        return await @this.RecreateLogAsync(@this.Services.GetService<IServiceDiscovery>(), @this.Services.GetService<LoggingLevelSwitch>());
    }

    private static async Task<WebAssemblyHost> RecreateLogAsync(this WebAssemblyHost @this, IServiceDiscovery serviceDiscovery, LoggingLevelSwitch loggingLevelSwitch)
    {
        var configuration = new LoggerConfiguration()
            .MinimumLevel.ControlledBy(loggingLevelSwitch)
            .Enrich.WithProperty("Application", "MyApp")
            .WriteTo.BrowserConsole(levelSwitch: loggingLevelSwitch);

        try
        {
            var serviceEndPoint = await serviceDiscovery.GetServiceEndPoint("ingest");
            if (!string.IsNullOrWhiteSpace(serviceEndPoint))
            {
                var ingestUrl = serviceEndPoint.TrimEnd('/') + "/ingest";
                configuration = configuration.WriteTo.BrowserHttp($"{ingestUrl}", controlLevelSwitch: loggingLevelSwitch);
                Log.Information("Configured Log for with server side ingest '{IngestUrl}'", ingestUrl);
            }
            else
            {
                Log.Warning("Ingest server endpoint not found");
            }
        }
        catch (Exception e)
        {
            Log.Warning(e, "Error recreating Log for with server side ingest");
        }

        Log.Logger = configuration.CreateLogger();

        return @this;
    }
 }

Results:

The code above works but breaks the NavigationManager. When the NavigateTo is called, nothing happens.

alexfdezsauco avatar May 15 '20 23:05 alexfdezsauco