Skip to content

Commit 73792de

Browse files
fix: add try catch to Init UpdateCfg (#313)
1 parent 73c557b commit 73792de

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/BlazorApplicationInsights/Components/ApplicationInsightsInit.razor.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
using System.Text.Json;
1+
using System;
2+
using System.Text.Json;
23
using System.Text.Json.Serialization;
34
using System.Threading.Tasks;
45
using BlazorApplicationInsights.Interfaces;
56
using Microsoft.AspNetCore.Components;
7+
using Microsoft.Extensions.Logging;
68
using Microsoft.JSInterop;
79

810
namespace BlazorApplicationInsights;
@@ -15,6 +17,7 @@ public partial class ApplicationInsightsInit
1517
[Inject] IApplicationInsights ApplicationInsights { get; set; }
1618
[Inject] private IJSRuntime JSRuntime { get; set; }
1719
[Inject] private ApplicationInsightsInitConfig Config { get; set; }
20+
[Inject] private ILogger<ApplicationInsightsInit> Logger { get; set; }
1821

1922
/// <summary>
2023
/// Must be enabled when running in Blazor Wasm Standalone
@@ -46,9 +49,15 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
4649

4750
if (Config.Config != null)
4851
{
49-
await ApplicationInsights.UpdateCfg(Config.Config, false);
50-
51-
await ApplicationInsights.TrackPageView();
52+
try
53+
{
54+
await ApplicationInsights.UpdateCfg(Config.Config, false);
55+
await ApplicationInsights.TrackPageView();
56+
}
57+
catch (Exception ex)
58+
{
59+
Logger.LogError(ex, "Failed to update configuration and track page view. An ad blocker or CSP may be blocking the App Insights script.");
60+
}
5261
}
5362
}
5463

0 commit comments

Comments
 (0)