Scrivere codice per tenere traccia delle richieste con Application Insights

Application Insights deve tenere traccia delle richieste per l'applicazione per fornire profili per l'applicazione nella pagina Prestazioni nel portale di Azure. Per le applicazioni basate su framework già instrumentati (ad esempio ASP.NET e ASP.NET Core), Application Insights può tenere traccia automatica delle richieste.

Per altre applicazioni, ad esempio ruoli di lavoro di Azure Servizi cloud e API senza stato di Azure Service Fabric, è necessario tenere traccia delle richieste con codice che indica a Application Insights dove iniziano e terminano le richieste. Le richieste di telemetria vengono quindi inviate a Application Insights, che è possibile visualizzare nella pagina Prestazioni . I profili vengono raccolti per tali richieste.

Per tenere traccia manuale delle richieste:

  1. Aggiungere il codice seguente in un punto iniziale della durata dell'applicazione:

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

    Per altre informazioni sulla configurazione della chiave di strumentazione globale, vedere Using Service Fabric with Application Insights (Usare Service Fabric con Application Insights).

  2. Aggiungere un'istruzione usingStartOperation<RequestTelemetry> intorno a ogni parte del codice che si vuole instrumentare, come nell'esempio seguente:

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

    La chiamata di StartOperation<RequestTelemetry> in un altro ambito di StartOperation<RequestTelemetry> non è supportata. È possibile invece usare StartOperation<DependencyTelemetry> nell'ambito nidificato. Ad esempio:

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

Nota

Il 31 marzo 2025, il supporto per l'inserimento delle chiavi di strumentazione terminerà. L'inserimento delle chiavi di strumentazione continuerà a funzionare, ma non verranno più forniti aggiornamenti o supporto per la funzionalità. Passare alle stringhe di connessione per sfruttare le nuove funzionalità.

Passaggi successivi

Risolvere i problemi relativi al profiler di Application Insights.