BlazorApplicationInsights
BlazorApplicationInsights copied to clipboard
SetInstrumentationKey doesn't appear to be working (Gives 'Please provide instrumentation key')
I checked out your Sample project and just replaced the cfg in index.html with an empty object.
Like so: cfg: {}
Then I went into Program.cs, removed the telemetry, and added SetInstrumentationKey
builder.Services.AddBlazorApplicationInsights(async applicationInsights =>
{
await applicationInsights.SetInstrumentationKey("MyKeyHere");
await applicationInsights.LoadAppInsights();
});
but I keep getting this warning in the browser
Failed to initialize AppInsights JS SDK for instance appInsights - Please provide instrumentation key
If I replace it directly in the index.html it works fine, but I'm trying to get the key from the appsettings since that's what gets replaced in my deployment scripts.
I tried with both the original .NET Core version the library was written in, and also the one I'm using (5.0.4), but the problem appears in both.
I should note that I'm using Blazor WebAssembly
So after a bit of further testing it appears that the ApplicationInsights are being loaded before the JSInterop for this library is called, even though I have ld set to -1.
Any update here? I have the same issue.
Sadly not. As far as my testing could conclude, the problem is that AppInsights's js code was always being executed before page load (regardless of the -1). So I ended up setting a static load delay of 500ms which allows the interop to inject the key before the js loads. The drawback here is that it won't capture startup/pageload errors, but It's better than nothing. (For those wondering why I didn't just add a placeholder value there and then replace it on deploy in the pipeline. I considered this and talked it over with my supervisor but he decided against it).
Thanks for your update. What I can say too is even if there is this warning in the browser App Insights get events from the web app.
I have updated the readme with a new approach on how to set the instrumentation key. See, https://github.com/IvanJosipovic/BlazorApplicationInsights#set-instrumentation-key-programmatically
Thanks, I updated to 1.6 (was 1.5) modified the js in index and the code in program.cs but it still doesn't work (I'm on Net 6). Looking in Console/Network I can see that the iKey sent is empty ("").
I ran into a similar issue. On opening the WebApp a request to https://dc.services.visualstudio.com/v2/track gets made with iKey
being an empty string. Any subsequent requests do have the correct value for iKey
.
I've added a script load delay of 400ms which seems to fix this issue for the time being. Any ideas on how else to fix this? I'd rather not be dependant on an artificial delay as that might not work for all users.
I recently noticed, that while the warning remains, it does appear that with a delay of -1 everything is still transmitted to AppInsights. Because I originally tried with a delay of 500 and even a second, but on a free App Service plan this already failed.
If I use a delay of -1 the first request with Pageview and PageviewPerformance will fail and the next request succeeds but sends the first failure to AppInsights
"data": {
"baseType": "MessageData",
"baseData": {
"ver": 2,
"message": "AI (Internal): 100 message:\"Invalid Instrumentation key \"",
"properties": {}
}
}
When adding the 400ms delay it will send the correct date for Pageview and PageviewPerformance.
So using no delay results in 2 missed messages on opening the webapp + 1 error message that should not happen. Using the delay results in the expected 2 messages to appear in AppInsights.
Adding a delay also seems to fix another issue, the name
and refUri
only have the correct values with a delay.
Using the default Blazor Wasm template Without delay:
"data": {
"baseType": "PageviewData",
"baseData": {
"ver": 2,
"name": "BlazorApp1",
"url": "http://localhost:5279/",
"duration": "00:00:00.226",
"properties": {
"refUri": ""
},
"measurements": {},
"id": "244a38de3c9344ed9351045b821a3843"
}
}
With delay:
"data": {
"baseType": "PageviewData",
"baseData": {
"ver": 2,
"name": "Index",
"url": "http://localhost:5279/",
"duration": "00:00:00.249",
"properties": {
"refUri": "http://localhost:5279/"
},
"measurements": {},
"id": "244a38de3c9344ed9351045b821a3843"
}
}
We setup our solution according to Set Instrumentation Key Programmatically from the readme. And our calls to AI are always with ikey=""

and this of course returns a 400
code in program.cs, and key is correctly filled in
var key = builderApp.Configuration.GetSection("ApplicationInsights").GetSection("InstrumentationKey").Value;
builderApp.Services.AddBlazorApplicationInsights(async applicationInsights =>
{
await applicationInsights.SetInstrumentationKey(key);
await applicationInsights.TrackPageView();
});
and then all needed scripts and imports in index.cshtml
we are on .NET (core) 6 and the 1.6 version of your package
a setup of the not working sample solution can be found here https://github.com/bc3/aiblazor-InsightsTest/
I have documented a new approach for Blazor WASM ASP.NET Hosted Projects in the Readme. This solution will need to be manually implemented for self hosted Blazor, it essentially uses a cookie to store the ConnectionString.
I do not recommend using SetInstrumentationKey and SetConnectionString (due to https://github.com/IvanJosipovic/BlazorApplicationInsights/issues/90) and they will likely be marked as obsolete in a future version.