private void LoadConfiguration(ILogger logger) { // Populate Logging info to ITelemetry var populateTypeConfig = new AssemblyInfoConfig(); var populateTelemetrySection = _config.GetSection(AppInsightsLoggingConfig.ApplicationInsightsSection) .GetSection(AppInsightsLoggingConfig.PopulateTelemetry); foreach (var populateItem in populateTelemetrySection.GetChildren()) { populateItem.Bind(populateTypeConfig); } // Using Reflection, get the IPopulateTelemetry function var factoryType = Type.GetType(populateTypeConfig.ClassAssembly, throwOnError: true); PopulateLogTelemetry = Activator.CreateInstance(factoryType) as IPopulateTelemetry; // Read Logging Levels var levels = _config.GetSection(AppInsightsLoggingConfig.ApplicationInsightsSection)? .GetSection(LoggingSettingsConfig.LogLevelSection); var dictLogLevel = new Dictionary <string, LogLevel>(); LogLevel defaultLevel = LogLevel.Warning; if (levels != null) { foreach (var k in levels.GetChildren()) { LogLevel level; if (Enum.TryParse(k.Value, true, out level)) { dictLogLevel.Add(k.Key, level); } else { dictLogLevel.Add(k.Key, LogLevel.Warning); } } if (dictLogLevel.ContainsKey("Default")) { defaultLevel = dictLogLevel["Default"]; dictLogLevel.Remove("Default"); } } _levels = dictLogLevel.ToImmutableDictionary(); _defaultLevel = defaultLevel; }
public AppInsightElasticSearchSink( ITelemetryProcessor next, IConfiguration config, ILogger logger) : base(config, logger, nameof(AppInsightElasticSearchSink)) { _next = next; // Reflection: Create Instance of ITransformOpenSchema var openSchemaConfig = new AssemblyInfoConfig(); config.GetSection(ElasticSearchSinkConfig.TransformOutputSection).Bind(openSchemaConfig); var factoryType = Type.GetType(openSchemaConfig.ClassAssembly, throwOnError: true); _transformOpenSchema = Activator.CreateInstance(factoryType) as ITransformOutput; }
/// <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(); }