Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Application Insights 需要跟踪应用程序的请求,从而能在 Azure 门户的性能概述页面上为应用程序提供性能分析。 对于基于已检测的框架(如 ASP.NET 和 ASP.NET Core)构建的应用程序,Application Insights 可以自动跟踪请求。
对于其他应用程序(如 Azure Service Fabric 无状态 API),需要使用代码跟踪请求,告知 Application Insights 请求的开始和结束位置。 然后,请求的遥测数据被发送到 Application Insights。 可以在 “性能 ”页上查看该遥测数据。 为这些请求收集概要文件。
要手动跟踪请求:
在应用程序生命周期的早期添加以下代码:
using Microsoft.ApplicationInsights.Extensibility; ... // Replace with your own Application Insights connection string. TelemetryConfiguration.Active.InstrumentationKey = "00000000-0000-0000-0000-000000000000";有关此全局连接字符串配置的详细信息,请参阅 将 Service Fabric 与 Application Insights 配合使用。
对于要检测的任何代码片段,请在其周围添加
StartOperation<RequestTelemetry>using语句,如以下示例所示:using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.DataContracts; ... var client = new TelemetryClient(); ... using (var operation = client.StartOperation<RequestTelemetry>("Insert_Your_Custom_Event_Unique_Name")) { // ... Code I want to profile. }调用
StartOperation<RequestTelemetry>在其他StartOperation<RequestTelemetry>作用域中不受支持。 可以改为在嵌套作用域中使用StartOperation<DependencyTelemetry>。 例如:using (var getDetailsOperation = client.Operation<RequestTelemetry>("GetProductDetails")) { try { ProductDetail details = new ProductDetail() { Id = productId }; getDetailsOperation.Telemetry.Properties["ProductId"] = productId.ToString(); // By using DependencyTelemetry, 'GetProductPrice' is correctly linked as part of the 'GetProductDetails' request. using (var getPriceOperation = client.StartOperation<DependencyTelemetry>("GetProductPrice")) { double price = await _priceDataBase.GetAsync(productId); if (IsTooCheap(price)) { throw new PriceTooLowException(productId); } details.Price = price; } // Similarly, note how 'GetProductReviews' doesn't establish another RequestTelemetry. using (var getReviewsOperation = client.StartOperation<DependencyTelemetry>("GetProductReviews")) { details.Reviews = await _reviewDataBase.GetAsync(productId); } getDetailsOperation.Telemetry.Success = true; return details; } catch(Exception ex) { getDetailsOperation.Telemetry.Success = false; // This exception gets linked to the 'GetProductDetails' request telemetry. client.TrackException(ex); throw; } }