Mengaktifkan Snapshot Debugger untuk aplikasi .NET di Azure Service Fabric, Cloud Service, dan Virtual Machines

Jika aplikasi ASP.NET atau ASP.NET Core Anda berjalan di Azure App Service, sangat disarankan untuk mengaktifkan Snapshot Debugger melalui halaman portal Application Insights. Tetapi, jika aplikasi Anda memerlukan konfigurasi Snapshot Debugger yang disesuaikan, atau versi pratinjau .NET core, maka instruksi ini harus diikuti selain instruksi untuk mengaktifkan melalui halaman portal Application Insights.

Jika aplikasi Anda berjalan di Azure Service Fabric, Cloud Service, Virtual Machines, atau mesin lokal, petunjuk berikut harus digunakan.

Mengonfigurasi koleksi rekam jepret untuk aplikasi ASP.NET

  1. Aktifkan Application Insights di aplikasi web Anda, jika Anda belum melakukannya.

  2. Sertakan paket NuGet Microsoft.ApplicationInsights.SnapshotCollector di aplikasi Anda.

  3. Jika perlu, sesuaikan konfigurasi Snapshot Debugger yang ditambahkan ke ApplicationInsights.config. Konfigurasi Snapshot Debugger default sebagian besar kosong dan semua pengaturan bersifat opsional. Berikut adalah contoh yang menunjukkan konfigurasi yang setara dengan konfigurasi default:

    <TelemetryProcessors>
        <Add Type="Microsoft.ApplicationInsights.SnapshotCollector.SnapshotCollectorTelemetryProcessor, Microsoft.ApplicationInsights.SnapshotCollector">
        <!-- The default is true, but you can disable Snapshot Debugging by setting it to false -->
        <IsEnabled>true</IsEnabled>
        <!-- Snapshot Debugging is usually disabled in developer mode, but you can enable it by setting this to true. -->
        <!-- DeveloperMode is a property on the active TelemetryChannel. -->
        <IsEnabledInDeveloperMode>false</IsEnabledInDeveloperMode>
        <!-- How many times we need to see an exception before we ask for snapshots. -->
        <ThresholdForSnapshotting>1</ThresholdForSnapshotting>
        <!-- The maximum number of examples we create for a single problem. -->
        <MaximumSnapshotsRequired>3</MaximumSnapshotsRequired>
        <!-- The maximum number of problems that we can be tracking at any time. -->
        <MaximumCollectionPlanSize>50</MaximumCollectionPlanSize>
        <!-- How often we reconnect to the stamp. The default value is 15 minutes.-->
        <ReconnectInterval>00:15:00</ReconnectInterval>
        <!-- How often to reset problem counters. -->
        <ProblemCounterResetInterval>1.00:00:00</ProblemCounterResetInterval>
        <!-- The maximum number of snapshots allowed in ten minutes.The default value is 1. -->
        <SnapshotsPerTenMinutesLimit>3</SnapshotsPerTenMinutesLimit>
        <!-- The maximum number of snapshots allowed per day. -->
        <SnapshotsPerDayLimit>30</SnapshotsPerDayLimit>
        <!-- Whether or not to collect snapshot in low IO priority thread. The default value is true. -->
        <SnapshotInLowPriorityThread>true</SnapshotInLowPriorityThread>
        <!-- Agree to send anonymous data to Microsoft to make this product better. -->
        <ProvideAnonymousTelemetry>true</ProvideAnonymousTelemetry>
        <!-- The limit on the number of failed requests to request snapshots before the telemetry processor is disabled. -->
        <FailedRequestLimit>3</FailedRequestLimit>
        </Add>
    </TelemetryProcessors>
    
  4. Rekam jepret hanya dikumpulkan pada pengecualian yang dilaporkan ke Application Insights. Dalam beberapa kasus (misalnya, versi lama platform .NET), Anda mungkin perlu mengonfigurasi koleksi pengecualian untuk melihat pengecualian dengan rekam jepret di portal.

Mengonfigurasi koleksi snapshot untuk aplikasi menggunakan ASP.NET Core LTS atau lebih tinggi

  1. Aktifkan Application Insights di aplikasi web ASP.NET Core Anda, jika Anda belum melakukannya.

    Catatan

    Pastikan bahwa aplikasi Anda mereferensikan versi 2.1.1, atau yang lebih baru, dari paket Microsoft.ApplicationInsights.AspNetCore.

  2. Sertakan paket NuGet Microsoft.ApplicationInsights.SnapshotCollector di aplikasi Anda.

  3. Ubah kelas Startup aplikasi Anda untuk menambahkan dan mengonfigurasi prosesor telemetri Snapshot Collector.

    1. Jika paket NuGet Microsoft.ApplicationInsights.SnapshotCollector versi 1.3.5 atau lebih tinggi digunakan, tambahkan pernyataan penggunaan berikut ke Startup.cs.

           using Microsoft.ApplicationInsights.SnapshotCollector;
      

      Tambahkan yang berikut ini di akhir metode ConfigureServices di kelas Startup di Startup.cs.

           services.AddSnapshotCollector((configuration) => Configuration.Bind(nameof(SnapshotCollectorConfiguration), configuration));
      
    2. Jika paket NuGet Microsoft.ApplicationInsights.SnapshotCollector versi 1.3.4 atau lebih rendah digunakan, tambahkan pernyataan penggunaan berikut ke Startup.cs.

      using Microsoft.ApplicationInsights.SnapshotCollector;
      using Microsoft.Extensions.Options;
      using Microsoft.ApplicationInsights.AspNetCore;
      using Microsoft.ApplicationInsights.Extensibility;
      

      Tambahkan kelas SnapshotCollectorTelemetryProcessorFactory berikut ke kelas Startup.

      class Startup
      {
          private class SnapshotCollectorTelemetryProcessorFactory : ITelemetryProcessorFactory
          {
              private readonly IServiceProvider _serviceProvider;
      
              public SnapshotCollectorTelemetryProcessorFactory(IServiceProvider serviceProvider) =>
                  _serviceProvider = serviceProvider;
      
              public ITelemetryProcessor Create(ITelemetryProcessor next)
              {
                  var snapshotConfigurationOptions = _serviceProvider.GetService<IOptions<SnapshotCollectorConfiguration>>();
                  return new SnapshotCollectorTelemetryProcessor(next, configuration: snapshotConfigurationOptions.Value);
              }
          }
          ...
      

      Tambahkan layanan SnapshotCollectorConfiguration dan SnapshotCollectorTelemetryProcessorFactory ke alur startup:

         // This method gets called by the runtime. Use this method to add services to the container.
         public void ConfigureServices(IServiceCollection services)
         {
             // Configure SnapshotCollector from application settings
             services.Configure<SnapshotCollectorConfiguration>(Configuration.GetSection(nameof(SnapshotCollectorConfiguration)));
      
             // Add SnapshotCollector telemetry processor.
             services.AddSingleton<ITelemetryProcessorFactory>(sp => new SnapshotCollectorTelemetryProcessorFactory(sp));
      
             // TODO: Add other services your application needs here.
         }
      }
      
  4. Jika diperlukan, sesuaikan konfigurasi Snapshot Debugger dengan menambahkan bagian SnapshotCollectorConfiguration ke appsettings.json. Semua pengaturan dalam konfigurasi Snapshot Debugger bersifat opsional. Berikut adalah contoh yang menunjukkan konfigurasi yang setara dengan konfigurasi default:

    {
      "SnapshotCollectorConfiguration": {
        "IsEnabledInDeveloperMode": false,
        "ThresholdForSnapshotting": 1,
        "MaximumSnapshotsRequired": 3,
        "MaximumCollectionPlanSize": 50,
        "ReconnectInterval": "00:15:00",
        "ProblemCounterResetInterval":"1.00:00:00",
        "SnapshotsPerTenMinutesLimit": 1,
        "SnapshotsPerDayLimit": 30,
        "SnapshotInLowPriorityThread": true,
        "ProvideAnonymousTelemetry": true,
        "FailedRequestLimit": 3
      }
    }
    

Mengonfigurasi koleksi rekam jepret untuk aplikasi .NET lainnya

  1. Jika aplikasi Anda belum dilengkapi dengan Application Insights, mulailah dengan mengaktifkan Application Insights dan atur kunci instrumentasi.

  2. Tambahkan paket NuGet Microsoft.ApplicationInsights.SnapshotCollector di aplikasi Anda.

  3. Rekam jepret hanya dikumpulkan pada pengecualian yang dilaporkan ke Application Insights. Anda mungkin perlu mengubah kode untuk melaporkannya. Kode penanganan pengecualian tergantung pada struktur aplikasi Anda, tetapi contohnya ada di bawah:

    TelemetryClient _telemetryClient = new TelemetryClient();
    
    void ExampleRequest()
    {
        try
        {
            // TODO: Handle the request.
        }
        catch (Exception ex)
        {
            // Report the exception to Application Insights.
            _telemetryClient.TrackException(ex);
    
            // TODO: Rethrow the exception if desired.
        }
    }
    

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.

Langkah berikutnya

  • Hasilkan lalu lintas ke aplikasi Anda yang dapat memicu pengecualian. Kemudian, tunggu 10 hingga 15 menit agar rekam jepret dikirim ke instans Application Insights.
  • Lihat rekam jepret di portal Microsoft Azure.
  • Untuk bantuan tentang pemecahan masalah Snapshot Debugger, lihat Pemecahan masalah Snapshot Debugger.