question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not working with Blazor application

See original GitHub issue

I configured the Blazor app that comes with .NET CORE 3.0 in VS2019, but no log is generated 😦

Added in Counter.razor page:

@code {
    int currentCount = 0;

    void IncrementCount()
    {
        currentCount++;
        throw new InvalidOperationException("Test");
     }
}

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
conficientcommented, Sep 1, 2022

Had a quick look at this again. The new <ErrorBoundary> component in .NET 6 can be useful here. It normally is used to wrap components that might have issues on a page, but it’s possible (but not advisable?) to wrap the whole App in one.

The component itself does not allow intercepting the error to log it, but you can create a subclass which can:

    public class GlobalErrorHandler : ErrorBoundary
    {
        protected override Task OnErrorAsync(Exception exception)
        {
            // log to ELMAH here
            ElmahExtensions.RaiseError(exception);
            return base.OnErrorAsync(exception);
        }
    }

You would then wrap the router in the App.razor in this:

<GlobalErrorHandler>
    <ChildContent>
        <Router AppAssembly="@typeof(App).Assembly">
            <Found Context="routeData">
                <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
                <FocusOnNavigate RouteData="@routeData" Selector="h1" />
            </Found>
            <NotFound>
                <PageTitle>Not found</PageTitle>
                <LayoutView Layout="@typeof(MainLayout)">
                    <p role="alert">Sorry, there's nothing at this address.</p>
                </LayoutView>
            </NotFound>
        </Router>
    </ChildContent>
    <ErrorContent>
        <p>Sorry an error occurred</p>
    </ErrorContent>
</GlobalErrorHandler>

Worth experimenting with but there may be issues with this approach I’ve not considered.

0reactions
skcsknathan001commented, Aug 31, 2022

Elmah logs unhandled server errors. If you already do try/catch, it won’t. However, you can log manually in your catch block like ElmahExtensions.RaiseError(ex);

Read more comments on GitHub >

github_iconTop Results From Across the Web

My deployed Blazor application only shows “Loading ...
My deployed Blazor application only shows “Loading…” Why is the deployed Blazor application not working? ... You must update the virtual app path...
Read more >
net 6 blazor wasm hot reload not working
I'm using Visual Studio 2022 and created a new Blazor webassembly app and ran it. The option for hot reload on file save...
Read more >
Asp.net core web application BLAZOR not starting
Empty new BLAZOR project. Press F5, web browser appaers and waiting. After some time it closes. Event log shows error: Application ...
Read more >
Blazor app crashes when debugging after windows ...
Blazor app crashes when debugging after windows secutiry updates ... A free and open-source web framework that enables developers to create web ...
Read more >
Blazor server app not working when deployed, works fine ...
I have a Blazor server-side project which I've been developing in Visual Studio 2019, using .NET5. All has been working fine. I've just...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found