Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blazor client-side Application level exception handling

How to globally handle application level exceptions for client-side Blazor apps?

like image 759
AlvinfromDiaspar Avatar asked May 23 '19 02:05

AlvinfromDiaspar


People also ask

How do you handle exceptions in Blazor?

Blazor treats most unhandled exceptions as fatal to the circuit where they occur. If a circuit is terminated due to an unhandled exception, the user can only continue to interact with the app by reloading the page to create a new circuit.

Why is Blazor not popular?

Blazor projects are slow on the client-side because you have to download the entire dot net runtime along with the necessary DLL libraries on your browser. Additionally, Blazor apps have latency issues.

What are the limitations of Blazor?

The Blazor WebAssembly hosting model has the following limitations: The app is restricted to the capabilities of the browser. Capable client hardware and software (for example, WebAssembly support) is required. Download size is larger, and apps take longer to load.

Is Blazor client production ready?

Blazor currently has two hosting models that are production-ready.


2 Answers

You can create a singleton service that handles the WriteLine event. This will be fired only on errors thanks to Console.SetError(this);

public class ExceptionNotificationService : TextWriter
{
    private TextWriter _decorated;
    public override Encoding Encoding => Encoding.UTF8;

    public event EventHandler<string> OnException;

    public ExceptionNotificationService()
    {
        _decorated = Console.Error;
        Console.SetError(this);
    }

    public override void WriteLine(string value)
    {
        OnException?.Invoke(this, value);

        _decorated.WriteLine(value);
    }
}

You then add it to the Startup.cs file in the ConfigureServices function:

services.AddSingleton<ExceptionNotificationService>();

To use it you just subscribe to the OnException event in your main view.

Source

like image 82
Gerrit Avatar answered Dec 21 '22 19:12

Gerrit


@Gerrit's answer is not up to date. Now you should use ILogger for handling unhandled exception.

My example

public interface IUnhandledExceptionSender
{
    event EventHandler<Exception> UnhandledExceptionThrown;
}

public class UnhandledExceptionSender : ILogger, IUnhandledExceptionSender
{

    public event EventHandler<Exception> UnhandledExceptionThrown;

    public IDisposable BeginScope<TState>(TState state)
    {
        return null;
    }

    public bool IsEnabled(LogLevel logLevel)
    {
        return true;
    }

    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state,
        Exception exception, Func<TState, Exception, string> formatter)
    {            
        if (exception != null)
        {                
            UnhandledExceptionThrown?.Invoke(this, exception);
        }            
    }
}

Program.cs

var unhandledExceptionSender = new UnhandledExceptionSender();
var myLoggerProvider = new MyLoggerProvider(unhandledExceptionSender);
builder.Logging.AddProvider(myLoggerProvider);
builder.Services.AddSingleton<IUnhandledExceptionSender>(unhandledExceptionSender);
like image 37
3per Avatar answered Dec 21 '22 18:12

3per