From 75e6498033a88fe8e19f0ba3c65fe7d2a9fad380 Mon Sep 17 00:00:00 2001 From: Jesse Swart Date: Mon, 30 Dec 2024 13:25:37 +0100 Subject: [PATCH] Add additional telemetry configuration options --- src/MauiInsights/MauiAppBuilderExtensions.cs | 41 +++++++++++++++---- src/MauiInsights/MauiInsightsConfiguration.cs | 1 + 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/MauiInsights/MauiAppBuilderExtensions.cs b/src/MauiInsights/MauiAppBuilderExtensions.cs index 4fec3f0..e973a5d 100644 --- a/src/MauiInsights/MauiAppBuilderExtensions.cs +++ b/src/MauiInsights/MauiAppBuilderExtensions.cs @@ -1,5 +1,6 @@ using MauiInsights.CrashHandling; using Microsoft.ApplicationInsights; +using Microsoft.ApplicationInsights.AspNetCore; using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Http; @@ -13,15 +14,17 @@ namespace MauiInsights; public static class MauiAppBuilderExtensions { private static TelemetryClient? _client; - public static MauiAppBuilder AddApplicationInsights(this MauiAppBuilder appBuilder, string appInsightsConnectionString) => AddApplicationInsights(appBuilder, new MauiInsightsConfiguration { ApplicationInsightsConnectionString = appInsightsConnectionString }); + public static MauiAppBuilder AddApplicationInsights(this MauiAppBuilder appBuilder, string appInsightsConnectionString, Action? configureTelemetry = null) => AddApplicationInsights(appBuilder, new MauiInsightsConfiguration { ApplicationInsightsConnectionString = appInsightsConnectionString }, configureTelemetry); - public static MauiAppBuilder AddApplicationInsights(this MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration) + public static MauiAppBuilder AddApplicationInsights(this MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration, Action? configureTelemetry = null) { if (string.IsNullOrEmpty(configuration.ApplicationInsightsConnectionString)) { throw new ArgumentException("Configuration must have a valid Application Insights Connection string", nameof(configuration)); } - SetupTelemetryClient(appBuilder, configuration); + configureTelemetry ??= _ => { }; + + SetupTelemetryClient(appBuilder, configuration, configureTelemetry); SetupHttpDependecyTracking(appBuilder); SetupPageViewTelemetry(); SetupLogger(appBuilder, configuration); @@ -106,9 +109,10 @@ private static void SendCrashes(IConnectivity connectivity, CrashLogger crashLog }); } - private static void SetupTelemetryClient(MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration) + private static void SetupTelemetryClient(MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration, Action configureTelemetry) { - var telemetryConfiguration = GetTelemetryConfiguration(configuration); + var telemetryConfiguration = GetTelemetryConfiguration(appBuilder, configuration); + configureTelemetry(telemetryConfiguration); _client = new TelemetryClient(telemetryConfiguration); appBuilder.Services.AddSingleton(_client); } @@ -132,7 +136,7 @@ private static void SetupPageViewTelemetry() private static void SetupLogger(MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration) { - var telemetryConfig = GetTelemetryConfiguration(configuration); + var telemetryConfig = GetTelemetryConfiguration(appBuilder, configuration); var logConfig = new ApplicationInsightsLoggerOptions() { FlushOnDispose = true, @@ -143,7 +147,7 @@ private static void SetupLogger(MauiAppBuilder appBuilder, MauiInsightsConfigura Options.Create(telemetryConfig), Options.Create(logConfig))); } - private static TelemetryConfiguration GetTelemetryConfiguration(MauiInsightsConfiguration configuration) + private static TelemetryConfiguration GetTelemetryConfiguration(MauiAppBuilder builder, MauiInsightsConfiguration configuration) { var telemetryConfig = new TelemetryConfiguration() { @@ -157,6 +161,29 @@ private static TelemetryConfiguration GetTelemetryConfiguration(MauiInsightsConf telemetryConfig.TelemetryInitializers.Add(initializer); } + var telemetryInitializers = builder.Services + .Where(s => s.ServiceType == typeof(ITelemetryInitializer) && (s.ImplementationInstance ?? s.KeyedImplementationInstance) is ITelemetryInitializer) + .Select(s => s.ImplementationInstance ?? s.KeyedImplementationInstance) + .Cast() + .ToList(); + foreach(var initializer in telemetryInitializers) + { + telemetryConfig.TelemetryInitializers.Add(initializer); + } + + var telemetryProcessors = + builder.Services.Where(s => s.ServiceType == typeof(ITelemetryProcessorFactory)).ToList(); + + var serviceProvider = builder.Services.BuildServiceProvider(); + + foreach (var processorDescriptor in telemetryProcessors) + { + if (processorDescriptor.ImplementationFactory is not Func + factoryMethod) continue; + var factory = factoryMethod(serviceProvider); + telemetryConfig.TelemetryProcessorChainBuilder.Use(factory.Create); + } + return telemetryConfig; } } \ No newline at end of file diff --git a/src/MauiInsights/MauiInsightsConfiguration.cs b/src/MauiInsights/MauiInsightsConfiguration.cs index 502ef08..869f1df 100644 --- a/src/MauiInsights/MauiInsightsConfiguration.cs +++ b/src/MauiInsights/MauiInsightsConfiguration.cs @@ -6,5 +6,6 @@ public class MauiInsightsConfiguration { public string? ApplicationInsightsConnectionString { get; set; } public IDictionary AdditionalTelemetryProperties { get; set; } = new Dictionary(); + [Obsolete("Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers")] public IEnumerable TelemetryInitializers { get; set; } = Enumerable.Empty(); }