Blazor global exception handler with current URL information

Viewed 38

I want to log the URL set in the browser when the unhandled exceptions occur in a Blazor Server application.

I am using Serilog for logging and also using the request logging middleware.

builder.Host.UseSerilog();
app.UseSerilogRequestLogging;

but this is not enough since there is just one HTTP request and then the Blazor SignalR connection is opened. Therefore the Serilog does not know the actual path in the browser set by Blazor and always returns /_blazor, which makes sense.

I don't want to wrap all my functions in try - catch blocks to log the exceptions. Also the Blazor kind of handles the exception and sets the CSS of the blazor-error-ui element.

To try to reproduce the error I need the URL that is set in the browser when the exception occurs. Is there a way to catch exceptions globally and somehow get the URL set in the browser? Maybe get access to the NavigationManager object?

0 Answers
Related