skip to Main Content

I dont know if it’s posssible to create full TelemetryConfiguration without applicationInsight.config

I see there are TelemetryInitializers.Add it working well

but nothing for the configs

  • TelemetryModules (present in config file but not on the class)
  • DefaultTelemetrySink.TelemetryProcessors (present in the config file but there are no Add method in the class)

I serarched everywhere for an example of complete configuration via code but did not find anything. I want to get rid of the config file, because I want to use the TelemetryConfiguration in a common library. And don’t want to pupulate many config files in end up librairies.

I looking in debug mode and seen many differences between class obtained with TelemetryConfiguration.CreateDefault() when I have the config file obtained from nuget package, and the one obtained when I have no config file. This is the reason of my question.

what I have sofar

internal class ApplicationInsightConfig
{
   public TelemetryConfiguration Config { get; }

   public ApplicationInsightConfig()
   {
      Config = TelemetryConfiguration.CreateDefault();
      Config.InstrumentationKey = ConfigurationManager.GetSetting(new ConfigKey("ApplicationInsightKey"));
      Config.ApplicationIdProvider = new ApplicationInsightsApplicationIdProvider();
      Config.TelemetryChannel.DeveloperMode = false;
      DefineTelemetryInitializers();
      DefineDefaultTelemetrySink();
   }

   private void DefineDefaultTelemetrySink()
   {
      var tp = Config.DefaultTelemetrySink.TelemetryProcessors;
      Config.DefaultTelemetrySink.TelemetryChannel = new ServerTelemetryChannel();
   }

   private void DefineTelemetryInitializers()
   {
      Config.TelemetryInitializers.Add(new AzureAppServiceRoleNameFromHostNameHeaderInitializer());
      Config.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer());
      Config.TelemetryInitializers.Add(new AzureRoleEnvironmentTelemetryInitializer());
      Config.TelemetryInitializers.Add(new BuildInfoConfigComponentVersionTelemetryInitializer());
      Config.TelemetryInitializers.Add(new WebTestTelemetryInitializer());
      Config.TelemetryInitializers.Add(new SyntheticUserAgentTelemetryInitializer { Filters = "search|spider|crawl|Bot|Monitor|AlwaysOn" });
      Config.TelemetryInitializers.Add(new ClientIpHeaderTelemetryInitializer());
      Config.TelemetryInitializers.Add(new OperationNameTelemetryInitializer());
      Config.TelemetryInitializers.Add(new UserTelemetryInitializer());
      Config.TelemetryInitializers.Add(new AuthenticatedUserIdTelemetryInitializer());
      Config.TelemetryInitializers.Add(new AccountIdTelemetryInitializer());
      Config.TelemetryInitializers.Add(new SessionTelemetryInitializer());
   }
}

Thank you for help!

2

Answers


  1. Chosen as BEST ANSWER

    For those how want te final version here is the code :

    //-------------------------------------------------------------
    // Transcription code of the default ApplicationInsight.config
    //-------------------------------------------------------------
    internal class ApplicationInsightConfig
    {
       public static TelemetryConfiguration Setup()
       {
          var config = TelemetryConfiguration.CreateDefault();
    
          config.InstrumentationKey = ConfigurationManager.GetSetting(new ConfigKey("ApplicationInsightKey"));
          config.TelemetryChannel = new ServerTelemetryChannel { DeveloperMode = false };
    
          //Setup TelemetryInitializers...
          InitTelemetryInitializer(config);
    
          //Setup Modules
          InitTelemetryModules(config);
    
          //Setup TelemetryProcessor
          InitTelemetryProcessor(config.TelemetryProcessorChainBuilder);
    
          return config;
       }
    
       private static void InitTelemetryProcessor(TelemetryProcessorChainBuilder builder)
       {
          builder.Use(next => new QuickPulseTelemetryProcessor(next));
          builder.Use(next => new AutocollectedMetricsExtractor(next));
          builder.Use(next => new AdaptiveSamplingTelemetryProcessor(next) { MaxTelemetryItemsPerSecond = 5, ExcludedTypes = "Event" });
          builder.Use(next => new AdaptiveSamplingTelemetryProcessor(next) {MaxTelemetryItemsPerSecond = 5, IncludedTypes = "Event"});
          builder.Build();
       }
    
       private static void InitTelemetryModules(TelemetryConfiguration config)
       {
          new DependencyTrackingTelemetryModule
          {
             ExcludeComponentCorrelationHttpHeadersOnDomains =
             {
                "core.windows.net",
                "core.chinacloudapi.cn",
                "core.cloudapi.de",
                "core.usgovcloudapi.net"
             },
             IncludeDiagnosticSourceActivities =
             {
                "Microsoft.Azure.EventHubs",
                "Microsoft.Azure.ServiceBus"
             }
          }.Initialize(config);
          new PerformanceCollectorModule().Initialize(config);
          new QuickPulseTelemetryModule().Initialize(config);
          new AppServicesHeartbeatTelemetryModule().Initialize(config);
          new AzureInstanceMetadataTelemetryModule().Initialize(config);
          new DeveloperModeWithDebuggerAttachedTelemetryModule().Initialize(config);
          new UnhandledExceptionTelemetryModule().Initialize(config);
          new UnobservedExceptionTelemetryModule().Initialize(config);
          new RequestTrackingTelemetryModule
          {
             Handlers =
             {
                "Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.RequestDataHttpHandler",
                "System.Web.StaticFileHandler",
                "System.Web.Handlers.AssemblyResourceLoader",
                "System.Web.Optimization.BundleHandler",
                "System.Web.Script.Services.ScriptHandlerFactory",
                "System.Web.Handlers.TraceHandler",
                "System.Web.Services.Discovery.DiscoveryRequestHandler",
                "System.Web.HttpDebugHandler"
             }
          }.Initialize(config);
          new ExceptionTrackingTelemetryModule().Initialize(config);
          new AspNetDiagnosticTelemetryModule().Initialize(config);
       }
    
       private static void InitTelemetryInitializer(TelemetryConfiguration config)
       {
          config.TelemetryInitializers.Add(new AzureAppServiceRoleNameFromHostNameHeaderInitializer());
          config.TelemetryInitializers.Add(new HttpDependenciesParsingTelemetryInitializer());
          config.TelemetryInitializers.Add(new AzureRoleEnvironmentTelemetryInitializer());
          config.TelemetryInitializers.Add(new BuildInfoConfigComponentVersionTelemetryInitializer());
          config.TelemetryInitializers.Add(new WebTestTelemetryInitializer());
          config.TelemetryInitializers.Add(new SyntheticUserAgentTelemetryInitializer { Filters = "search|spider|crawl|Bot|Monitor|AlwaysOn" });
          config.TelemetryInitializers.Add(new ClientIpHeaderTelemetryInitializer());
          config.TelemetryInitializers.Add(new OperationNameTelemetryInitializer());
          config.TelemetryInitializers.Add(new UserTelemetryInitializer());
          config.TelemetryInitializers.Add(new AuthenticatedUserIdTelemetryInitializer());
          config.TelemetryInitializers.Add(new AccountIdTelemetryInitializer());
          config.TelemetryInitializers.Add(new SessionTelemetryInitializer());
       }
    }
    

  2. I think you are on the right track. Setting the telemetry channel could be done slightly different:

    TelemetryConfiguration config = TelemetryConfiguration.CreateDefault();
    config.InstrumentationKey = ConfigurationManager.GetSetting(new ConfigKey("ApplicationInsightKey"));
    config.TelemetryChannel = new ServerTelemetryChannel { DeveloperMode = false };
    

    Telemetry modules all implement ITelemetryModule, which defines a void Initialize(TelemetryConfiguration configuration) method to wire up the module:

    var module = new UnhandledExceptionTelemetryModule();
    module.Initialize(config);
    

    Telemetry processors can be added using the TelemetryProcessorChainBuilder:

    var builder = config.TelemetryProcessorChainBuilder;
    builder.Use(next => new AdaptiveSamplingTelemetryProcessor(next));
    builder.Build();
    

    Then the final step is to use the config:

    var telemetryClient = new TelemetryClient(config);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search