0

Any idea on how to get the line number of an unhanded exception in blazor webassembly?

There was a discussion long ago on some work that still needs to be done by the team to have it working. I think that died down if I'm not mistaken.

Consider the message below. It leaves one completely in the dark with no guidance on where to start looking.

Thanks in advance.

enter image description here

4

1 回答 1

0

不要粗鲁,但在 Blazor 文档中,总是在与组件交互时自己处理异常:https ://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/handle-errors?view= aspnetcore-5.0&pivots=webassembly#global-exception-handling

只需将所有方法体(甚至生命周期方法)都用一个try/catch块包装起来。注入ILogger<MyComponent>友好日志的奖励积分。例子:

@inject ILogger<MyComponent> Logger

<h1>@UserName</h1>

@code {
    private string UserName { get; set; } = string.Empty;
    protected override async Task OnInitializedAsync()
    {
        try
        {
            NavigationManager.LocationChanged += OnLocationChanged;
            var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            UserName = $"Hi, {authState.User.Claims.FirstOrDefault(x => x.Type == "display_name")?.Value}!";
        }
        catch (Exception ex)
        {
            Logger.LogError($"Failed to initialize MyComponent. Error: {ex}");
        }
    }
}
于 2021-03-18T19:50:26.290 回答