Добавление настраиваемых свойств в телеметрию запроса по умолчанию

Как я могу добавить настраиваемые свойства к телеметрии запроса по умолчанию в аналитике приложений? Мне удалось это сделать, создав новый клиент телеметрии, но я бы НЕ хотел этого делать, поскольку он создает повторяющиеся события.


person user2630162    schedule 03.09.2015    source источник


Ответы (1)


Создайте свой собственный TelemetryInitializer. https://azure.microsoft.com/en-us/documentation/articles/app-insights-api-custom-events-metrics/#telemetry-initializers.

вырезано из статьи выше:

using System;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;

namespace MvcWebRole.Telemetry
{
  /*
   * Custom TelemetryInitializer that overrides the default SDK 
   * behavior of treating response codes >= 400 as failed requests
   * 
   */
  public class MyTelemetryInitializer : ITelemetryInitializer
  {
    public void Initialize(ITelemetry telemetry)
    {
        var requestTelemetry = telemetry as RequestTelemetry;
        // Is this a TrackRequest() ?
        if (requestTelemetry == null) return;
        int code;
        bool parsed = Int32.TryParse(requestTelemetry.ResponseCode, out code);
        if (!parsed) return;
        if (code >= 400 && code < 500)
        {
            // If we set the Success property, the SDK won't change it:
            requestTelemetry.Success = true;
            // Allow us to filter these requests in the portal:
            requestTelemetry.Context.Properties["Overridden400s"] = "true";
        }
        // else leave the SDK to set the Success property      
    }
  }
}

затем загрузите этот инициализатор либо в файл конфигурации, либо с помощью кода, подробности см. в документе выше.

person BrettJ    schedule 03.09.2015