azure 创建不带配置文件的ApplicationInsight TelemetryConfiguration

ovfsdjhp  于 2023-08-07  发布在  其他
关注(0)|答案(3)|浏览(86)

我不知道是否有可能在没有applicationInsight.config的情况下创建完整的遥测配置
我看到有遥测初始化器。添加它工作正常
但不适用于配置

  • 遥测模块(存在于配置文件中,但不存在于类中)
  • DefaultTelemetrySink.TelemetryProcessors(存在于配置文件中,但类中没有Add方法)

我到处搜索通过代码完成配置的示例,但没有找到任何东西。我想去掉这个配置文件,因为我想在一个公共库中使用TelemetryConfiguration。而且不想在最终库中发布许多配置文件。
我在调试模式下查看了一下,发现当我有从nuget包中获取的配置文件时,使用**TelemetryConfiguration.CreateDefault()**获取的类与没有配置文件时获取的类之间有许多差异。这就是我提出问题的原因。
我目前所拥有的

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());
   }
}

字符串
谢谢你帮忙!

pgvzfuti

pgvzfuti1#

我想你的方向是对的。设置遥测通道的方法略有不同:

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

字符串
遥测模块都实现了ITelemetryModule,它定义了一个void Initialize(TelemetryConfiguration configuration)方法来连接模块:

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


可以使用TelemetryProcessorChainBuilder添加遥测处理器:

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


最后一步是使用配置:

var telemetryClient = new TelemetryClient(config);

93ze6v8z

93ze6v8z2#

类的最新版本

public static class ApplicationInsightConfig
{
   internal static TelemetryConfiguration Setup()
   {
      var config = TelemetryConfiguration.CreateDefault();

      config.ConnectionString = GetConnectionString();
      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());
   }

   public static string GetConnectionString()
   {
      var configKey = ConfigurationManager.GetSetting(new ConfigKey("ApplicationInsightKey"));

      if (string.IsNullOrEmpty(configKey)) configKey = "local";
      return $"InstrumentationKey={configKey};";
   } 
}

字符串

eyh26e7m

eyh26e7m3#

对于那些想要最终版本的人,这里是代码:

using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.Implementation;
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector;
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
using Microsoft.ApplicationInsights.Web;
using Microsoft.ApplicationInsights.WindowsServer;
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;

//-------------------------------------------------------------
// 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());
   }
}

字符串
封装Nuget:

  • Microsoft.ApplicationInsights.DependencyCollector(2.21.0+)
  • Microsoft.ApplicationInsights.PerfCounterCollector(2.21.0+)
  • Microsoft.ApplicationInsights.TraceListener(2.21.0+)
  • Microsoft.ApplicationInsights.Web(2.21.0+)
  • Microsoft.ApplicationInsights.WindowsServer(2.21.0+)
  • Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel(2.21.0+)

相关问题