Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional telemetry configuration options #6

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions src/MauiInsights/MauiAppBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,15 +14,17 @@
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<TelemetryConfiguration>? 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<TelemetryConfiguration>? 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);
Expand Down Expand Up @@ -106,9 +109,10 @@
});
}

private static void SetupTelemetryClient(MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration)
private static void SetupTelemetryClient(MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration, Action<TelemetryConfiguration> configureTelemetry)
{
var telemetryConfiguration = GetTelemetryConfiguration(configuration);
var telemetryConfiguration = GetTelemetryConfiguration(appBuilder, configuration);
configureTelemetry(telemetryConfiguration);
_client = new TelemetryClient(telemetryConfiguration);
appBuilder.Services.AddSingleton(_client);
}
Expand All @@ -132,7 +136,7 @@

private static void SetupLogger(MauiAppBuilder appBuilder, MauiInsightsConfiguration configuration)
{
var telemetryConfig = GetTelemetryConfiguration(configuration);
var telemetryConfig = GetTelemetryConfiguration(appBuilder, configuration);
var logConfig = new ApplicationInsightsLoggerOptions()
{
FlushOnDispose = true,
Expand All @@ -143,7 +147,7 @@
Options.Create(telemetryConfig), Options.Create(logConfig)));
}

private static TelemetryConfiguration GetTelemetryConfiguration(MauiInsightsConfiguration configuration)
private static TelemetryConfiguration GetTelemetryConfiguration(MauiAppBuilder builder, MauiInsightsConfiguration configuration)
{
var telemetryConfig = new TelemetryConfiguration()
{
Expand All @@ -152,11 +156,34 @@
telemetryConfig.TelemetryInitializers.Add(new AdditionalPropertiesInitializer(configuration.AdditionalTelemetryProperties));
telemetryConfig.TelemetryInitializers.Add(new ApplicationInfoInitializer());

foreach(var initializer in configuration.TelemetryInitializers)

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'

Check warning on line 159 in src/MauiInsights/MauiAppBuilderExtensions.cs

View workflow job for this annotation

GitHub Actions / build

'MauiInsightsConfiguration.TelemetryInitializers' is obsolete: 'Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers'
{
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<ITelemetryInitializer>()
.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<IServiceProvider, ITelemetryProcessorFactory>
factoryMethod) continue;
var factory = factoryMethod(serviceProvider);
telemetryConfig.TelemetryProcessorChainBuilder.Use(factory.Create);
}

return telemetryConfig;
}
}
1 change: 1 addition & 0 deletions src/MauiInsights/MauiInsightsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class MauiInsightsConfiguration
{
public string? ApplicationInsightsConnectionString { get; set; }
public IDictionary<string, string> AdditionalTelemetryProperties { get; set; } = new Dictionary<string, string>();
[Obsolete("Use the AddApplicationInsights with the configureTelemetry parameter instead to add ITelemetryInitializers")]
public IEnumerable<ITelemetryInitializer> TelemetryInitializers { get; set; } = Enumerable.Empty<ITelemetryInitializer>();
}
Loading