我有一个服务器端 blazor 应用,我想使用 Azure 应用程序见解对其进行监视。目前,我只看到在启动应用程序并关闭它时发送的请求,两者之间没有任何内容......我找不到任何支持Blazor的微软文档。
是否可以让 Azure 应用程序见解与服务器端 Blazor 完全配合使用?如果是这样,有谁知道任何关于这个的指南?
您有两种方法-如果您计划迁移到WASM版本,则基于JavaScript的方法非常理想,因为它需要很少的更改即可转换,但是为了解决@liero的问题,我还在底部包含了一种服务器端方法。
基于 JavaScript 的方法(Blazor WASM 或服务器端)
我们在 Blazor 服务器端使用社区提供的 BlazorApplicationInsights 包来满足此需求。是的,GitHub 存储库中的说明与 Blazor WASM 的说明相匹配,但我很乐意为您翻译(它在服务器端同样有效)。请注意,根据 Blazor 服务器端模板中使用的路径提供文件的路径。
此外,我还创建了一个 Git 存储库,其中包含一个实现以下步骤的项目,供您在此处查看。仍需要按照步骤 4 中指定的方式设置应用程序见解检测密钥才能正常工作。
-
将 NuGet 包
BlazorApplicationInsights
安装到 Blazor 服务器端项目。 -
在
ConfigureServices()
方法中的Startup.cs
中,添加以下行:
services.AddBlazorApplicationInsights();
您可能希望扩充设置以反映自定义角色和实例值。您可以改用以下内容来处理此问题(同一位置):
services.AddBlazorApplicationInsights(async appInsights => {
var telemetryItem = new TelemetryItem
{
Tags = new Dictionary<string, object>
{
{"ai.cloud.role", "SPA"},
{"ai.cloud.roleInstance", "Blazor server-side"}
}
};
await appInsights.AddTelemetryInitializer(telemetryItem);
});
- 将以下@using语句添加到
_Imports.razor
:
@using BlazorApplicationInsights
- 将对应用程序见解 JS 文件的引用添加到
Pages/_Host.cshtml
文件的头部。从这里提取的脚本。请注意末尾的行,需要将字符串值替换为应用程序见解检测密钥。
<head>
<!-- ... -->
<script type="text/javascript">
!function (T, l, y) { var S = T.location, k = "script", D = "instrumentationKey", C = "ingestionendpoint", I = "disableExceptionTracking", E = "ai.device.", b = "toLowerCase", w = "crossOrigin", N = "POST", e = "appInsightsSDK", t = y.name || "appInsights"; (y.name || T[e]) && (T[e] = t); var n = T[t] || function (d) { var g = !1, f = !1, m = { initialize: !0, queue: [], sv: "5", version: 2, config: d }; function v(e, t) { var n = {}, a = "Browser"; return n[E + "id"] = a[b](), n[E + "type"] = a, n["ai.operation.name"] = S && S.pathname || "_unknown_", n["ai.internal.sdkVersion"] = "javascript:snippet_" + (m.sv || m.version), { time: function () { var e = new Date; function t(e) { var t = "" + e; return 1 === t.length && (t = "0" + t), t } return e.getUTCFullYear() + "-" + t(1 + e.getUTCMonth()) + "-" + t(e.getUTCDate()) + "T" + t(e.getUTCHours()) + ":" + t(e.getUTCMinutes()) + ":" + t(e.getUTCSeconds()) + "." + ((e.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 } } } } var h = d.url || y.src; if (h) { function a(e) { var t, n, a, i, r, o, s, c, u, p, l; g = !0, m.queue = [], f || (f = !0, t = h, s = function () { var e = {}, t = d.connectionString; if (t) for (var n = t.split(";"), a = 0; a < n.length; a++) { var i = n[a].split("="); 2 === i.length && (e[i[0][b]()] = i[1]) } if (!e[C]) { var r = e.endpointsuffix, o = r ? e.location : null; e[C] = "https://" + (o ? o + "." : "") + "dc." + (r || "services.visualstudio.com") } return e }(), c = s[D] || d[D] || "", u = s[C], p = u ? u + "/v2/track" : d.endpointUrl, (l = []).push((n = "SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details)", a = t, i = p, (o = (r = v(c, "Exception")).data).baseType = "ExceptionData", o.baseData.exceptions = [{ typeName: "SDKLoadFailed", message: n.replace(/./g, "-"), hasFullStack: !1, stack: n + "nSnippet failed to load [" + a + "] -- Telemetry is disablednHelp Link: https://go.microsoft.com/fwlink/?linkid=2128109nHost: " + (S && S.pathname || "_unknown_") + "nEndpoint: " + i, parsedStack: [] }], r)), l.push(function (e, t, n, a) { var i = v(c, "Message"), r = i.data; r.baseType = "MessageData"; var o = r.baseData; return o.message = 'AI (Internal): 99 message:"' + ("SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details) (" + n + ")").replace(/"/g, "") + '"', o.properties = { endpoint: a }, i }(0, 0, t, p)), function (e, t) { if (JSON) { var n = T.fetch; if (n && !y.useXhr) n(t, { method: N, body: JSON.stringify(e), mode: "cors" }); else if (XMLHttpRequest) { var a = new XMLHttpRequest; a.open(N, t), a.setRequestHeader("Content-type", "application/json"), a.send(JSON.stringify(e)) } } }(l, p)) } function i(e, t) { f || setTimeout(function () { !t && m.core || a() }, 500) } var e = function () { var n = l.createElement(k); n.src = h; var e = y[w]; return !e && "" !== e || "undefined" == n[w] || (n[w] = e), n.onload = i, n.onerror = a, n.onreadystatechange = function (e, t) { "loaded" !== n.readyState && "complete" !== n.readyState || i(0, t) }, n }(); y.ld < 0 ? l.getElementsByTagName("head")[0].appendChild(e) : setTimeout(function () { l.getElementsByTagName(k)[0].parentNode.appendChild(e) }, y.ld || 0) } try { m.cookie = l.cookie } catch (p) { } function t(e) { for (; e.length;)!function (t) { m[t] = function () { var e = arguments; g || m.queue.push(function () { m[t].apply(m, e) }) } }(e.pop()) } var n = "track", r = "TrackPage", o = "TrackEvent"; t([n + "Event", n + "PageView", n + "Exception", n + "Trace", n + "DependencyData", n + "Metric", n + "PageViewPerformance", "start" + r, "stop" + r, "start" + o, "stop" + o, "addTelemetryInitializer", "setAuthenticatedUserContext", "clearAuthenticatedUserContext", "flush"]), m.SeverityLevel = { Verbose: 0, Information: 1, Warning: 2, Error: 3, Critical: 4 }; var s = (d.extensionConfig || {}).ApplicationInsightsAnalytics || {}; if (!0 !== d[I] && !0 !== s[I]) { var c = "onerror"; t(["_" + c]); var u = T[c]; T[c] = function (e, t, n, a, i) { var r = u && u(e, t, n, a, i); return !0 !== r && m["_" + c]({ message: e, url: t, lineNumber: n, columnNumber: a, error: i }), r }, d.autoExceptionInstrumented = !0 } return m }(y.cfg); function a() { y.onInit && y.onInit(n) } (T[t] = n).queue && 0 === n.queue.length ? (n.queue.push(a), n.trackPageView({})) : a() }(window, document, {
src: "https://js.monitor.azure.com/scripts/b/ai.2.min.js", // The SDK URL Source
// name: "appInsights", // Global SDK Instance name defaults to "appInsights" when not supplied
// ld: 0, // Defines the load delay (in ms) before attempting to load the sdk. -1 = block page load and add to head. (default) = 0ms load after timeout,
// useXhr: 1, // Use XHR instead of fetch to report failures (if available),
crossOrigin: "anonymous", // When supplied this will add the provided value as the cross origin attribute on the script tag
// onInit: null, // Once the application insights instance has loaded and initialized this callback function will be called with 1 argument -- the sdk instance (DO NOT ADD anything to the sdk.queue -- As they won't get called)
cfg: { // Application Insights Configuration
instrumentationKey: "INSTRUMENTATION_KEY"
}
});
</script>
</head>
- 将 JS 互操作添加到
Pages/_Host.cshtml
文件底部的结束</body>
标记之前:
<body>
<!-- ... ->
<script src="_content/BlazorApplicationInsights/JsInterop.js"></script>
</body>
- 将以下内容添加到
App.razor
文件的顶部以支持自动导航跟踪:
<ApplicationInsightsComponent />
- 此库将在路由更改时自动触发"跟踪页面浏览量",但其他任何内容都需要您手动触发事件。下面是您的某个页面中可能有一个示例(在此处查看更多示例方法):
@page "/"
@inject IApplicationInsights AppInsights
<button class="btn btn-primary" @onclick="TrackEvent">Track Event</button>
<button class="btn btn-secondary" @onclick="TrackMetric">Track Metric</button>
@code {
private async Task TrackEvent()
{
await AppInsights.TrackEvent("My Event");
}
private async Task TrackMetric()
{
await AppInsights.TrackMetric("myMetric", 100, 200, 1, 200, new Dictionary<string,object> {{"customProperty", "customValue"}});
await AppInsights.Flush();
}
}
您将注意到使用示例代码中的PageViewPerformanceTelemtry对TrackPageViewPerformance的支持。您可能希望在@code块中调用它,而不是绑定到按钮,如下所示:
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await AppInsights.TrackPageViewPerformance(new PageViewPerformanceTelemetry());
}
应用程序见解 SDK 方法(Blazor 服务器端)
将 NuGet 包
Microsoft.ApplicationInsights.AspNetCore
安装到 Blazor 服务器端项目。在
ConfigureServices()
方法中的Startup.cs
中,添加以下行,将字符串参数替换为应用见解检测密钥:
services.AddBlazorApplicationInsights("INSERT YOUR INSTRUMENTATION KEY HERE");
或者,您可以消除字符串并将密钥存储在以下任一环境变量中:
- APPINSIGHTS_INSTRUMENTATIONKEY
- 应用程序见解:检测键
可以在此处找到其他文档,其中介绍了可以在此处传递的有关您可能希望启用的其他遥测模块的各种选项。
- 将以下
@using
语句添加到_Imports.razor
:
@using Microsoft.ApplicationInsights
- 由于应用程序见解不具有与 Blazor 的任何本机集成,因此需要自行挂接要跟踪的所有内容。我们将在本节的其余部分介绍执行此操作的一些方法。
首先,我们需要创建主机组件来跟踪各种导航操作。在共享目录中创建一个名为ApplicationInsightsComponent.cs
的新 CS 文件,并填充以下内容:
using System;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Routing;
namespace ApplicationInsightsServerSample.Shared
{
public class ApplicationInsightsComponent : ComponentBase, IDisposable
{
[Inject]
private TelemetryClient _telemetryClient { get; init; }
[Inject]
private NavigationManager _navigationManager { get; init; }
protected override void OnAfterRender(bool firstRender)
{
if (firstRender)
{
_navigationManager.LocationChanged += NavigationManagerOnLocationChanged;
}
}
private void NavigationManagerOnLocationChanged(object? sender, LocationChangedEventArgs e)
{
_telemetryClient.TrackPageView(e.Location); //Set the argument to whatever you'd like to name the page
}
public void Dispose()
{
_navigationManager.LocationChanged -= NavigationManagerOnLocationChanged;
}
}
}
在App.razor
文件中,将以下内容添加到顶部:
<ApplicationInsightsComponent />
让我们也实现一些东西来捕获应用中引发的任何异常。 创建 共享目录中的另一个名为Error.razor
的文件,并填充以下内容:
@inject TelemetryClient _telemetryClient
<CascadingValue Value=this>
@ChildContent
</CascadingValue>
@code
{
[Parameter]
public RenderFragment ChildContent { get; set; }
public void ProcessError(Exception ex)
{
_telemetryClient.TrackException(ex);
}
}
返回到App.razor
文件,使用此新组件包装路由器,如下所示:
<Error>
<Router ...>
...
</Router>
</Error>
然后,在任何组件中,将 Error 组件指定为代码块中的级联参数,如下所示:
@code {
[CascadingParameter]
public Error Error {get; set;}
}
当您使用 try/catch 块包装操作时,请在 catch 中调用Error.ProcessError(ex)
以将异常传播到我们的错误组件:
try
{
// ...
}
catch (Exception ex)
{
Error.ProcessError(ex);
}
除此之外,您无疑还希望跟踪各个页面上任意数量的事件,因此我将以 TrackEvent 示例结束。与另一个答案一样,让我们重新利用页面示例及其按钮,并在此处重新处理以兼容:
@page "/"
@inject TelemetryClient _telemetryClient
<button class="btn btn-primary" @onclick="TrackEvent">Track Event</button>
<button class="btn btn-secondary" @onclick="TrackMetric">Track Metric</button>
@code {
private void TrackEvent()
{
await _telemetryClient.TrackEvent("My Event");
}
private void TrackMetric()
{
_telemetryClient.TrackMetric("myMetric", 100, new Dictionary<string,string> {{"customProperty", "customValue"}});
}
}
我希望这有所帮助,但我很高兴用您正在寻找的任何其他细节更新此答案!
Blazor 服务器应用本质上是一个 asp.net 核心应用。因此,应按照本指南设置应用见解集成。
- 在项目中添加"Microsoft.ApplicationInsights.AspNetCore"nuget。
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.13.1" />
</ItemGroup>
- 在启动方法中添加
services.AddApplicationInsightsTelemetry();
ConfigureServices
.cs。
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// The following line enables Application Insights telemetry collection.
services.AddApplicationInsightsTelemetry();
// This code adds other services for your application.
...
}
- 在appsettins.json中添加检测密钥。
{
"ApplicationInsights": {
"InstrumentationKey": "putinstrumentationkeyhere"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
注意:客户端日志记录目前不支持开箱即用。 https://github.com/microsoft/ApplicationInsights-dotnet/issues/2143。但是,您可以使用社区项目 https://github.com/IvanJosipovic/BlazorApplicationInsights