Skip to content

Commit

Permalink
ConnectionString can be null so it can be set in "configure provider"
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Oct 11, 2024
1 parent e393cd4 commit 784172f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 11 additions & 7 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace TinyInsights;

public class ApplicationInsightsProvider : IInsightsProvider, ILogger
{
private readonly string connectionString;
private string? ConnectionString { get; set; }
private static ApplicationInsightsProvider? provider;

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ApplicationInsightsProvider.provider' is never assigned to, and will always have its default value null

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ApplicationInsightsProvider.provider' is never assigned to, and will always have its default value null

Check warning on line 14 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

Field 'ApplicationInsightsProvider.provider' is never assigned to, and will always have its default value null
private const string userIdKey = nameof(userIdKey);

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions TinyInsights/InsightsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder)
return appBuilder;
}

public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder, string applicationInsightsConnectionString, Action<IInsightsProvider>? configureProvider = null)
public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder, string? applicationInsightsConnectionString = null, Action<IInsightsProvider>? configureProvider = null)
{
appBuilder.Services.AddSingleton<IInsights>((_) =>
{
Expand All @@ -38,7 +38,7 @@ public static MauiAppBuilder UseTinyInsights(this MauiAppBuilder appBuilder, str
return appBuilder;
}

public static MauiAppBuilder UseTinyInsightsAsILogger(this MauiAppBuilder appBuilder, string applicationInsightsConnectionString, Action<IInsightsProvider>? configureProvider = null)
public static MauiAppBuilder UseTinyInsightsAsILogger(this MauiAppBuilder appBuilder, string? applicationInsightsConnectionString = null, Action<IInsightsProvider>? configureProvider = null)
{
appBuilder.Services.AddSingleton<ILogger>((_) =>
{
Expand Down

0 comments on commit 784172f

Please sign in to comment.