gpt4 book ai didi

exception - Blazor 客户端应用程序级异常处理

转载 作者:行者123 更新时间:2023-12-04 20:27:20 24 4
gpt4 key购买 nike

如何全局处理客户端 Blazor 应用程序的应用程序级异常?

最佳答案

您可以创建一个处理 WriteLine 事件的单例服务。由于 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);
}
}

然后将其添加到 ConfigureServices 函数中的 Startup.cs 文件中:
services.AddSingleton<ExceptionNotificationService>();

要使用它,您只需在主 View 中订阅 OnException 事件。

Source

关于exception - Blazor 客户端应用程序级异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56267387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com