diff --git a/TinyInsights/ApplicationInsightsProvider.cs b/TinyInsights/ApplicationInsightsProvider.cs index 42b21b6..6842c42 100644 --- a/TinyInsights/ApplicationInsightsProvider.cs +++ b/TinyInsights/ApplicationInsightsProvider.cs @@ -10,7 +10,7 @@ namespace TinyInsights; public class ApplicationInsightsProvider : IInsightsProvider, ILogger { - private readonly string connectionString; + private string? ConnectionString { get; set; } private static ApplicationInsightsProvider? provider; private const string userIdKey = nameof(userIdKey); @@ -30,9 +30,9 @@ public class ApplicationInsightsProvider : IInsightsProvider, ILogger #if IOS || MACCATALYST || ANDROID - public ApplicationInsightsProvider(string connectionString) + public ApplicationInsightsProvider(string? connectionString = null) { - this.connectionString = connectionString; + ConnectionString = connectionString; provider = this; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; @@ -56,9 +56,9 @@ void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs } #elif WINDOWS - public ApplicationInsightsProvider(MauiWinUIApplication app, string connectionString) + public ApplicationInsightsProvider(MauiWinUIApplication app, string? connectionString = null) { - this.connectionString = connectionString; + ConnectionString = connectionString; provider = this; app.UnhandledException += App_UnhandledException; @@ -74,7 +74,6 @@ void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionE #elif NET8_0_OR_GREATER public ApplicationInsightsProvider() { - connectionString = string.Empty; // Do nothing. The net8.0 target exists for enabling unit testing, not for actual use. } #endif @@ -122,9 +121,14 @@ private static void OnAppearing(object? sender, Page e) try { + if (string.IsNullOrWhiteSpace(ConnectionString)) + { + throw new ArgumentNullException("ConnectionString", "ConnectionString is required to initialize TinyInsights"); + } + var configuration = new TelemetryConfiguration() { - ConnectionString = connectionString + ConnectionString = ConnectionString }; client = new TelemetryClient(configuration); diff --git a/TinyInsights/InsightsExtension.cs b/TinyInsights/InsightsExtension.cs index 57b7be7..f0c05c2 100644 --- a/TinyInsights/InsightsExtension.cs +++ b/TinyInsights/InsightsExtension.cs @@ -11,7 +11,7 @@ public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder) return appBuilder; } - public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder, string applicationInsightsConnectionString, Action? configureProvider = null) + public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder, string? applicationInsightsConnectionString = null, Action? configureProvider = null) { appBuilder.Services.AddSingleton((_) => { @@ -38,7 +38,7 @@ public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder, str return appBuilder; } - public static MauiAppBuilder UseTinyInsightsAsILogger(this MauiAppBuilder appBuilder, string applicationInsightsConnectionString, Action? configureProvider = null) + public static MauiAppBuilder UseTinyInsightsAsILogger(this MauiAppBuilder appBuilder, string? applicationInsightsConnectionString = null, Action? configureProvider = null) { appBuilder.Services.AddSingleton((_) => {