You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When combining BlazorApplicationInsights with Serilog in a Blazor WASM Standalone, the module is loaded too late and results in an exception on load. A full reproduction can be found here: https://github.com/vertonghenb/AnalyticsBug based on the steps below.
Possible solution:
Use JavaScript initializers so that the module is always loaded and initialised before Blazor even starts. The first thing we want to do is track the analytics, since when something goes wrong we have an exception for it in the logs. I think it's worthwhile to consider initialising BlazorAnalytics before the App actually starts. This can be done based on the article: https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0#javascript-initializers
and using a BlazorApplicationInsights.lib.module.js in the wwwroot folder in the base package
<scripttype="text/javascript">!(function(cfg){functione(){cfg.onInit&&cfg.onInit(i)}varS,u,D,t,n,i,C=window,x=document,w=C.location,I="script",b="ingestionendpoint",E="disableExceptionTracking",A="ai.device.";"instrumentationKey"[S="toLowerCase"](),u="crossOrigin",D="POST",t="appInsightsSDK",n=cfg.name||"appInsights",(cfg.name||C[t])&&(C[t]=n),i=C[n]||function(l){vard=!1,g=!1,f={initialize:!0,queue:[],sv:"7",version:2,config:l};functionm(e,t){varn={},i="Browser";functiona(e){e=""+e;return1===e.length?"0"+e:e}returnn[A+"id"]=i[S](),n[A+"type"]=i,n["ai.operation.name"]=w&&w.pathname||"_unknown_",n["ai.internal.sdkVersion"]="javascript:snippet_"+(f.sv||f.version),{time:(i=newDate).getUTCFullYear()+"-"+a(1+i.getUTCMonth())+"-"+a(i.getUTCDate())+"T"+a(i.getUTCHours())+":"+a(i.getUTCMinutes())+":"+a(i.getUTCSeconds())+"."+(i.getUTCMilliseconds()/1e3).toFixed(3).slice(2,5)+"Z",iKey:e,name:"Microsoft.ApplicationInsights."+e.replace(/-/g,"")+"."+t,sampleRate:100,tags:n,data:{baseData:{ver:2}},ver:4,seq:"1",aiDataContract:undefined}}varh=-1,v=0,y=["js.monitor.azure.com","js.cdn.applicationinsights.io","js.cdn.monitor.azure.com","js0.cdn.applicationinsights.io","js0.cdn.monitor.azure.com","js2.cdn.applicationinsights.io","js2.cdn.monitor.azure.com","az416426.vo.msecnd.net"],k=l.url||cfg.src;if(k){if((n=navigator)&&(~(n=(n.userAgent||"").toLowerCase()).indexOf("msie")||~n.indexOf("trident/"))&&~k.indexOf("ai.3")&&(k=k.replace(/(\/)(ai\.3\.)([^\d]*)$/,function(e,t,n){returnt+"ai.2"+n})),!1!==cfg.cr)for(vare=0;e<y.length;e++)if(0<k.indexOf(y[e])){h=e;break}vari=function(e){vara,t,n,i,o,r,s,c,p,u;f.queue=[],g||(0<=h&&v+1<y.length?(a=(h+v+1)%y.length,T(k.replace(/^(.*\/\/)([\w\.]*)(\/.*)$/,function(e,t,n,i){returnt+y[a]+i})),v+=1):(d=g=!0,o=k,c=(p=function(){vare,t={},n=l.connectionString;if(n)for(vari=n.split(";"),a=0;a<i.length;a++){varo=i[a].split("=");2===o.length&&(t[o[0][S]()]=o[1])}returnt[b]||(e=(n=t.endpointsuffix)?t.location:null,t[b]="https://"+(e?e+".":"")+"dc."+(n||"services.visualstudio.com")),t}()).instrumentationkey||l.instrumentationKey||"",p=(p=p[b])?p+"/v2/track":l.endpointUrl,(u=[]).push((t="SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details)",n=o,r=p,(s=(i=m(c,"Exception")).data).baseType="ExceptionData",s.baseData.exceptions=[{typeName:"SDKLoadFailed",message:t.replace(/\./g,"-"),hasFullStack:!1,stack:t+"\nSnippet failed to load ["+n+"] -- Telemetry is disabled\nHelp Link: https://go.microsoft.com/fwlink/?linkid=2128109\nHost: "+(w&&w.pathname||"_unknown_")+"\nEndpoint: "+r,parsedStack:[]}],i)),u.push((s=o,t=p,(r=(n=m(c,"Message")).data).baseType="MessageData",(i=r.baseData).message='AI (Internal): 99 message:"'+("SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details) ("+s+")").replace(/\"/g,"")+'"',i.properties={endpoint:t},n)),o=u,c=p,JSON&&((r=C.fetch)&&!cfg.useXhr?r(c,{method:D,body:JSON.stringify(o),mode:"cors"}):XMLHttpRequest&&((s=newXMLHttpRequest).open(D,c),s.setRequestHeader("Content-type","application/json"),s.send(JSON.stringify(o))))))},a=function(e,t){g||setTimeout(function(){!t&&f.core||i()},500),d=!1},T=function(e){varn=x.createElement(I),e=(n.src=e,cfg[u]);return!e&&""!==e||"undefined"==n[u]||(n[u]=e),n.onload=a,n.onerror=i,n.onreadystatechange=function(e,t){"loaded"!==n.readyState&&"complete"!==n.readyState||a(0,t)},cfg.ld&&cfg.ld<0?x.getElementsByTagName("head")[0].appendChild(n):setTimeout(function(){x.getElementsByTagName(I)[0].parentNode.appendChild(n)},cfg.ld||0),n};T(k)}try{f.cookie=x.cookie}catch(p){}functiont(e){for(;e.length;)!function(t){f[t]=function(){vare=arguments;d||f.queue.push(function(){f[t].apply(f,e)})}}(e.pop())}varr,s,n="track",o="TrackPage",c="TrackEvent",n=(t([n+"Event",n+"PageView",n+"Exception",n+"Trace",n+"DependencyData",n+"Metric",n+"PageViewPerformance","start"+o,"stop"+o,"start"+c,"stop"+c,"addTelemetryInitializer","setAuthenticatedUserContext","clearAuthenticatedUserContext","flush"]),f.SeverityLevel={Verbose:0,Information:1,Warning:2,Error:3,Critical:4},(l.extensionConfig||{}).ApplicationInsightsAnalytics||{});return!0!==l[E]&&!0!==n[E]&&(t(["_"+(r="onerror")]),s=C[r],C[r]=function(e,t,n,i,a){varo=s&&s(e,t,n,i,a);return!0!==o&&f["_"+r]({message:e,url:t,lineNumber:n,columnNumber:i,error:a,evt:C.event}),o},l.autoExceptionInstrumented=!0),f}(cfg.cfg),(C[n]=i).queue&&0===i.queue.length?(i.queue.push(e),i.trackPageView({})):e();})({src: "https://js.monitor.azure.com/scripts/b/ai.3.gbl.min.js",ld: -1,crossOrigin: "anonymous",cfg: {instrumentationKey: "00000000-0000-0000-0000-000000000000",disableTelemetry: true}});</script>
Add Logger on page
Home.razor.cs
[Inject]publicrequiredIApplicationInsightsAppInsights{get;set;}protectedoverridevoidOnInitialized(){Log.Information("Hello from Home");Log.Warning("Warning");Log.Error("Error");Log.Fatal("Fatal");}protectedoverrideasyncTaskOnAfterRenderAsync(boolfirstRender){base.OnAfterRender(firstRender);if(firstRender){awaitAppInsights.TrackEvent(newEventTelemetry{Name="FeaturePage"});awaitAppInsights.TrackMetric(newMetricTelemetry{Name="FeaturePage",Average=1});}}
Run the project shows the exception
blazor.webassembly.js:1
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Could not find 'blazorApplicationInsights.addTelemetryInitializer' ('blazorApplicationInsights' was undefined).
Error: Could not find 'blazorApplicationInsights.addTelemetryInitializer' ('blazorApplicationInsights' was undefined).
at https://localhost:7115/_framework/blazor.webassembly.js:1:368
at Array.forEach (<anonymous>)
at l.findFunction (https://localhost:7115/_framework/blazor.webassembly.js:1:336)
at w (https://localhost:7115/_framework/blazor.webassembly.js:1:5079)
at https://localhost:7115/_framework/blazor.webassembly.js:1:2872
at new Promise (<anonymous>)
at b.beginInvokeJSFromDotNet (https://localhost:7115/_framework/blazor.webassembly.js:1:2835)
at Object.gn [as invokeJSJson] (https://localhost:7115/_framework/blazor.webassembly.js:1:58932)
at https://localhost:7115/_framework/dotnet.runtime.8.0.2.gggw4qpwdt.js:3:177853
at Ul (https://localhost:7115/_framework/dotnet.runtime.8.0.2.gggw4qpwdt.js:3:178687)
Microsoft.JSInterop.JSException: Could not find 'blazorApplicationInsights.addTelemetryInitializer' ('blazorApplicationInsights' was undefined).
Error: Could not find 'blazorApplicationInsights.addTelemetryInitializer' ('blazorApplicationInsights' was undefined).
at https://localhost:7115/_framework/blazor.webassembly.js:1:368
at Array.forEach (<anonymous>)
at l.findFunction (https://localhost:7115/_framework/blazor.webassembly.js:1:336)
at w (https://localhost:7115/_framework/blazor.webassembly.js:1:5079)
at https://localhost:7115/_framework/blazor.webassembly.js:1:2872
at new Promise (<anonymous>)
at b.beginInvokeJSFromDotNet (https://localhost:7115/_framework/blazor.webassembly.js:1:2835)
at Object.gn [as invokeJSJson] (https://localhost:7115/_framework/blazor.webassembly.js:1:58932)
at https://localhost:7115/_framework/dotnet.runtime.8.0.2.gggw4qpwdt.js:3:177853
at Ul (https://localhost:7115/_framework/dotnet.runtime.8.0.2.gggw4qpwdt.js:3:178687)
at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__16`1[[Microsoft.JSInterop.Infrastructure.IJSVoidResult, Microsoft.JSInterop, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].MoveNext()
at Microsoft.JSInterop.JSRuntimeExtensions.InvokeVoidAsync(IJSRuntime jsRuntime, String identifier, Object[] args)
at BlazorApplicationInsights.ApplicationInsights.AddTelemetryInitializer(TelemetryItem telemetryItem)
at Program.<>c.<<<Main>$>b__0_2>d.MoveNext() in /Users/benjamin/Desktop/AnalyticsBug/Program.cs:line 28
--- End of stack trace from previous location ---
at BlazorApplicationInsights.ApplicationInsightsInit.OnAfterRenderAsync(Boolean firstRender)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
The text was updated successfully, but these errors were encountered:
vertonghenb
pushed a commit
to vertonghenb/BlazorApplicationInsights
that referenced
this issue
Mar 8, 2024
When combining BlazorApplicationInsights with Serilog in a Blazor WASM Standalone, the module is loaded too late and results in an exception on load. A full reproduction can be found here: https://github.com/vertonghenb/AnalyticsBug based on the steps below.
Possible solution:
Use JavaScript initializers so that the module is always loaded and initialised before Blazor even starts. The first thing we want to do is track the analytics, since when something goes wrong we have an exception for it in the logs. I think it's worthwhile to consider initialising BlazorAnalytics before the App actually starts. This can be done based on the article:
https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0#javascript-initializers
and using a
BlazorApplicationInsights.lib.module.js
in the wwwroot folder in the base packageSteps to reproduce
Create a new Blazor WASM Standalone (.NET8)
Adding Serilog.BrowserConsole
Program.cs
Adding BlazorApplicationInsights
Program.cs
_imports.razor
App.razor (top of file)
index.htnl (head)
Add Logger on page
Home.razor.cs
Run the project shows the exception
The text was updated successfully, but these errors were encountered: