Condividi tramite


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 del portale di Azure. Per le applicazioni basate su framework già instrumentati (ad esempio ASP.NET e ASP.NET Core), Application Insights può tenere traccia automaticamente delle richieste.

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

Per tenere traccia manualmente 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 saranno più garantiti aggiornamenti o supporto per la funzionalità. Eseguire la transizione alle stringhe di connessione per sfruttare le nuove funzionalità.

Passaggi successivi

Risolvere i problemi di Application Insights Profiler.