Azure Service Fabric, Cloud Services ve Sanal Makineler.NET uygulamaları için Snapshot Debugger'ı etkinleştirme

ASP.NET veya ASP.NET Core uygulamanız Azure Uygulaması Hizmeti'nde çalışıyorsa ve özelleştirilmiş bir Anlık Görüntü Hata Ayıklayıcısı yapılandırması veya .NET Core'un önizleme sürümü gerektiriyorsa, Azure Uygulaması Service'te .NET uygulamaları için Snapshot Debugger'ı etkinleştirme ile başlayın.

Uygulamanız Azure Service Fabric, Azure Cloud Services, Azure Sanal Makineler veya şirket içi makinelerde çalışıyorsa, App Service'te Snapshot Debugger'ı etkinleştirmeyi atlayabilir ve bu makaledeki yönergeleri izleyebilirsiniz.

Başlamadan önce

ASP.NET uygulamaları için anlık görüntü koleksiyonunu yapılandırma

Microsoft.Application'yi eklediğinizde Analizler. SnapshotCollector NuGet paketi uygulamanıza SnapshotCollectorTelemetryProcessor otomatik olarak TelemetryProcessors Application Analizler.config bölümüne eklenmelidir.

Application Analizler.config içinde görmüyorsanız SnapshotCollectorTelemetryProcessor veya Snapshot Debugger yapılandırmasını özelleştirmek istiyorsanız, el ile düzenleyebilirsiniz. Ancak, daha sonra Microsoft.Application Analizler'ın daha yeni bir sürümüne yükseltirseniz bu düzenlemelerin üzerine yazılabilir. SnapshotCollector NuGet paketi.

Aşağıdaki örnekte, varsayılan yapılandırmaya eşdeğer bir yapılandırma gösterilmektedir:

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

Anlık görüntüler yalnızca Uygulama Analizler bildirilen özel durumlarda toplanır. Bazı durumlarda (örneğin, .NET platformunun eski sürümleri), portalda anlık görüntüler içeren özel durumları görmek için özel durum koleksiyonunu yapılandırmanız gerekebilir.

ASP.NET Core uygulamaları veya Çalışan Hizmetleri için anlık görüntü koleksiyonunu yapılandırma

Önkoşullar

Uygulamanız zaten aşağıdaki Uygulama Analizler NuGet paketlerinden birine başvurmalıdır:

NuGet paketini ekleyin

Microsoft.Application Analizler ekleyin. Uygulamanıza SnapshotCollector NuGet paketi.

Hizmet koleksiyonunu güncelleştirme

Uygulamanızın hizmetlerin yapılandırıldığı başlangıç koduna uzantı yöntemine AddSnapshotCollector bir çağrı ekleyin. Çağrısından hemen sonra AddApplicationInsightsTelemetrybu satırı eklemek iyi bir fikirdir. Örneğin:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddSnapshotCollector();

Anlık Görüntü Toplayıcıyı yapılandırma

Çoğu durumda varsayılan ayarlar yeterlidir. Aksi takdirde, çağrısından önce aşağıdaki kodu ekleyerek ayarları özelleştirin AddSnapshotCollector()

using Microsoft.ApplicationInsights.SnapshotCollector;
...
builder.Services.Configure<SnapshotCollectorConfiguration>(builder.Configuration.GetSection("SnapshotCollector"));

Ardından, appsettings.json varsayılanları geçersiz kılabileceğiniz bir SnapshotCollector bölüm ekleyin. Aşağıdaki örnekte, varsayılan yapılandırmaya eşdeğer bir yapılandırma gösterilmektedir:

{
  "SnapshotCollector": {
    "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
  }
}

Appsettings.json kullanmadan Anlık Görüntü Toplayıcısı'nın davranışını el ile özelleştirmeniz gerekiyorsa, bunun bir temsilciyi alan aşırı yüklemesini AddSnapshotCollector kullanın. Örneğin:

builder.Services.AddSnapshotCollector(config => config.IsEnabledInDeveloperMode = true);

Diğer .NET uygulamaları için anlık görüntü koleksiyonunu yapılandırma

Anlık görüntüler yalnızca Uygulama Analizler bildirilen özel durumlarda toplanır. ASP.NET ve ASP.NET Core uygulamaları için, Application Analizler SDK'sı bir denetleyici yönteminden veya uç nokta yol işleyiciden kaçan işlenmeyen özel durumları otomatik olarak bildirir. Diğer uygulamalarda, kodunuzu rapor etmek için değiştirmeniz gerekebilir. Özel durum işleme kodu uygulamanızın yapısına bağlıdır. Bir örnek aşağıda verilmiştir:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

internal class ExampleService
{
  private readonly TelemetryClient _telemetryClient;

  public ExampleService(TelemetryClient telemetryClient)
  {
    // Obtain the TelemetryClient via dependency injection.
    _telemetryClient = telemetryClient;
  }

  public void HandleExampleRequest()
  {
    using IOperationHolder<RequestTelemetry> operation = 
        _telemetryClient.StartOperation<RequestTelemetry>("Example");
    try
    {
      // TODO: Handle the request.
      operation.Telemetry.Success = true;
    }
    catch (Exception ex)
    {
      // Report the exception to Application Insights.
      operation.Telemetry.Success = false;
      _telemetryClient.TrackException(ex);
      // TODO: Rethrow the exception if desired.
    }
  }
}

Aşağıdaki örnekte yerine TelemetryClientkullanılırILogger. Bu örnekte Uygulama Analizler Günlükçü Sağlayıcısı'nı kullandığınız varsayılır. Örnekte gösterildiği gibi, bir özel durum işlenirken, özel durumu ilk parametre olarak 'a geçirmeyi LogErrorunutmayın.

using Microsoft.Extensions.Logging;

internal class LoggerExample
{
  private readonly ILogger _logger;

  public LoggerExample(ILogger<LoggerExample> logger)
  {
    _logger = logger;
  }

  public void HandleExampleRequest()
  {
    using IDisposable scope = _logger.BeginScope("Example");
    try
    {
      // TODO: Handle the request
    }
    catch (Exception ex)
    {
      // Use the LogError overload with an Exception as the first parameter.
      _logger.LogError(ex, "An error occurred.");
    }
  }
}

Not

Varsayılan olarak, Application Analizler Günlükçü (ApplicationInsightsLoggerProvider) aracılığıyla özel durumları Snapshot Debugger'a iletirTelemetryClient.TrackException. Bu davranış, sınıfındaki TrackExceptionsAsExceptionTelemetry özelliği aracılığıyla denetlenmektedir ApplicationInsightsLoggerOptions . Uygulama Analizler Günlükçü'sini yapılandırırken olarak ayarlarsanız TrackExceptionsAsExceptionTelemetryfalse, yukarıdaki örnek Snapshot Debugger'ı tetiklemez. Bu durumda, kodunuzu el ile çağıracak TrackException şekilde değiştirin.

Not

31 Mart 2025’te izleme anahtarı alımı desteği sona erecektir. İzleme anahtarı alımı çalışmaya devam edecek, ancak artık özellik için güncelleştirme veya destek sağlamayacağız. Yeni özelliklerden yararlanmak için bağlantı dizesi geçiş.

Sonraki adımlar