.NET Application Insights Profiler는 Application Insights가 추적하는 요청에 대해서만 성능 프로필을 캡처합니다. 요청이 추적되지 않으면 Profiler가 프로파일링할 대상이 없으므로 Azure 포털의 Performance 페이지에 데이터가 표시되지 않습니다.
ASP.NET 및 ASP.NET Core처럼 이미 계측된 프레임워크를 기반으로 구축된 애플리케이션은 요청을 자동으로 추적하므로 추가 코드가 필요하지 않습니다. Azure Service Fabric 상태 비정상 API와 같은 다른 애플리케이션은 자체적으로 요청을 추적하지 않으므로 각 요청이 시작되고 끝나는 위치를 Application Insights에 알리기 위해 수동으로 계측해야 합니다.
이 문서에서는 연결 문자열 구성하고, 요청을 추적하는 작업을 만들고RequestTelemetry, 중첩된 작업을 DependencyTelemetry나타내는 방법을 보여 줍니다.
코드에서 수동으로 요청 추적
애플리케이션 코드에서 요청을 수동으로 추적하려면 다음을 수행합니다.
애플리케이션 수명 초기에 다음 코드를 추가합니다.
using Microsoft.ApplicationInsights.Extensibility; ... // Replace with your own Application Insights connection string. TelemetryConfiguration.Active.ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://<region>.in.applicationinsights.azure.com/";이 글로벌 연결 문자열 구성에 대한 자세한 내용은 Application Insights에서 Service Fabric 사용을 참조하세요.
계측할 코드 부분을 다음 예와 같이
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<RequestTelemetry>추적하고 중첩된 작업이 StartOperation<DependencyTelemetry> 부모 요청에 올바르게 연결되도록 각 중첩된 작업을 추적합니다.
다음 예에서는 GetProductDetails가 요청 원격 분석을 설정하고, 중첩된 GetProductPrice 및 GetProductReviews 작업은 DependencyTelemetry를 사용합니다.
TrackException는 예외를 요청에 연결합니다.GetProductDetails
using (var getDetailsOperation = client.StartOperation<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;
}
}