Tulis kode untuk melacak permintaan dengan Application Insights

Untuk melihat profil aplikasi Anda di halaman Performa, Azure Application Insights perlu melacak permintaan untuk aplikasi Anda. Application Insights dapat secara otomatis melacak permintaan untuk aplikasi yang dibangun di atas kerangka kerja yang sudah diinstrumentasi. Dua contoh adalah ASP.NET dan ASP.NET Core.

Untuk aplikasi lain, seperti peran pekerja Azure Cloud Services dan API stateless Service Fabric, Anda perlu menulis kode untuk memberi tahu Application Insights tempat permintaan Anda dimulai dan diakhiri. Setelah Anda menulis kode ini, permintaan telemetri dikirim ke Application Insights. Anda dapat melihat telemetri di halaman Performa, dan profil dikumpulkan untuk permintaan tersebut.

Catatan

Pada tanggal 31 Maret 2025, dukungan untuk penyerapan kunci instrumentasi akan berakhir. Penyerapan kunci instrumentasi akan berjalan terus, tetapi kami tidak akan lagi menyediakan pembaruan atau dukungan terhadap fitur tersebut. Transisi ke string koneksi untuk memanfaatkan kemampuan baru.

Untuk melacak permintaan secara manual, lakukan hal berikut:

  1. Di awal masa pakai aplikasi, tambahkan kode berikut:

    using Microsoft.ApplicationInsights.Extensibility;
    ...
    // Replace with your own Application Insights instrumentation key.
    TelemetryConfiguration.Active.InstrumentationKey = "00000000-0000-0000-0000-000000000000";
    

    Untuk informasi selengkapnya tentang konfigurasi kunci instrumentasi global ini, lihat Menggunakan Service Fabric dengan Application Insights.

  2. Untuk setiap bagian kode yang ingin Anda instrumen, tambahkan pernyataan StartOperation<RequestTelemetry>using di sekitarnya, seperti yang ditunjukkan pada contoh berikut:

    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.
    }
    

    Memanggil StartOperation<RequestTelemetry> ​​dalam cakupan StartOperation<RequestTelemetry> lain tidak didukung. Anda dapat menggunakan StartOperation<DependencyTelemetry> dalam cakupan berlapis sebagai gantinya. Contohnya:

    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;
    }
    }