Skip to content

Commit a1c3ccb

Browse files
Add PrerenderingErrorBoundaryLogger
1 parent 7d5b4eb commit a1c3ccb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Mvc/Mvc.ViewFeatures/src/DependencyInjection/MvcViewFeaturesMvcCoreBuilderExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.AspNetCore.Components.Lifetime;
99
using Microsoft.AspNetCore.Components.Rendering;
1010
using Microsoft.AspNetCore.Components.Routing;
11+
using Microsoft.AspNetCore.Components.Web;
1112
using Microsoft.AspNetCore.Mvc;
1213
using Microsoft.AspNetCore.Mvc.ApplicationModels;
1314
using Microsoft.AspNetCore.Mvc.ApplicationParts;
@@ -240,6 +241,7 @@ internal static void AddViewServices(IServiceCollection services)
240241
services.TryAddScoped<INavigationInterception, UnsupportedNavigationInterception>();
241242
services.TryAddScoped<ComponentApplicationLifetime>();
242243
services.TryAddScoped<ComponentApplicationState>(sp => sp.GetRequiredService<ComponentApplicationLifetime>().State);
244+
services.TryAddScoped<IErrorBoundaryLogger, PrerenderingErrorBoundaryLogger>();
243245

244246
services.TryAddTransient<ControllerSaveTempDataPropertyFilter>();
245247

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Components.Web;
7+
8+
namespace Microsoft.AspNetCore.Mvc.ViewFeatures
9+
{
10+
internal class PrerenderingErrorBoundaryLogger : IErrorBoundaryLogger
11+
{
12+
private static readonly Action<ILogger, string, Exception> _exceptionCaughtByErrorBoundary = LoggerMessage.Define<string>(
13+
LogLevel.Warning,
14+
100,
15+
"Unhandled exception rendering component: {Message}");
16+
17+
private readonly ILogger _logger;
18+
19+
public PrerenderingErrorBoundaryLogger(ILogger<ErrorBoundary> logger)
20+
{
21+
_logger = logger;
22+
}
23+
24+
public ValueTask LogErrorAsync(Exception exception, bool clientOnly)
25+
{
26+
_exceptionCaughtByErrorBoundary(_logger, exception.Message, exception);
27+
return ValueTask.CompletedTask;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)