public AppInsightsLoggerProvider( IConfiguration config, ILogger logger) { _config = config; _logger = logger; Client = new TelemetryClient(); var appInsightsLoggingConfig = new AppInsightsLoggingConfig(); config.GetSection(AppInsightsLoggingConfig.ApplicationInsightsSection).Bind(appInsightsLoggingConfig); Client.InstrumentationKey = appInsightsLoggingConfig.InstrumentationKey; // Load in the level map var change = config.GetReloadToken(); change.RegisterChangeCallback(ConfigUpdated, null); LoadConfiguration(_logger); }
/// <summary> /// Initialize AppInsights pipeline: read config and enable sinks for logging /// </summary> /// <param name="config"></param> /// <param name="initaInitializers"></param> /// <returns></returns> public static async Task Initialize(IConfiguration config, ILogger logger, IServiceProvider serviceProvider) { // AppInsights Config var configAppInsights = config.GetSection(AppInsightsLoggingConfig.ApplicationInsightsSection); var appInsightsLoggingConfig = new AppInsightsLoggingConfig(); configAppInsights.Bind(appInsightsLoggingConfig); // >>>>> Use the custom in-memory pipeline publishing channel // Config for InMemoryPublishingChannel var configInMemoryPublishingChannel = configAppInsights .GetSection(AppInsightsLoggingConfig.InMemoryPublishingChannelSection); TelemetryConfiguration.Active.TelemetryChannel = new InMemoryPublishingChannel( configInMemoryPublishingChannel, logger, new Uri(appInsightsLoggingConfig.TelemetryServiceEndpoint), CoreConstants.CustomPipelineKey); // >>>>> Add each ITelemetryInitializer loaded from config via Reflection and calling Factory for ITelemetryInitializer var telemetryInitializerSection = configAppInsights.GetSection(AppInsightsLoggingConfig.TelemetryInitializer); foreach (var initializerEntry in telemetryInitializerSection.GetChildren()) { var initializerEntryConfig = new AssemblyInfoConfig(); initializerEntry.Bind(initializerEntryConfig); ITelemetryInitializerFactory factory; try { // Using Reflection, get the ITelemetryInitializerFactory interface var factoryType = Type.GetType(initializerEntryConfig.ClassAssembly, throwOnError: true); factory = Activator.CreateInstance(factoryType) as ITelemetryInitializerFactory; ITelemetryInitializer telemetryInitializer = await factory.CreateInitializer(initializerEntry, logger, serviceProvider); TelemetryConfiguration.Active.TelemetryInitializers.Add(telemetryInitializer); } catch (Exception e) { logger?.LogError(0, e, nameof(Initialize)); } } // >>>>> Set up a custom app insights pipeline: add custom sinks var aiClientBuilder = TelemetryConfiguration.Active.TelemetryProcessorChainBuilder; // Get list from Config of IProcessorSinks, add them to the TelemetryProcessorChainBuilder var processorSinksSection = configAppInsights.GetSection(AppInsightsLoggingConfig.TelemetryProcessorSinks); foreach (var sinkSection in processorSinksSection.GetChildren()) { var sinkConfig = new AssemblyInfoConfig(); sinkSection.Bind(sinkConfig); ITelemetryProcessorSinkFactory factory; try { // Using Reflection, get the ITelemetryProcessorSinkFactory interface var factoryType = Type.GetType(sinkConfig.ClassAssembly, throwOnError: true); factory = Activator.CreateInstance(factoryType) as ITelemetryProcessorSinkFactory; await factory.UseProcessorSink(sinkSection, logger, aiClientBuilder); } catch (Exception e) { logger?.LogError(0, e, nameof(Initialize)); } } // Update the ai client configuration aiClientBuilder.Build(); }