From ccbc9b99c8f0903533664e97b33086c641ac9ba5 Mon Sep 17 00:00:00 2001 From: Daniel Hindrikes Date: Thu, 31 Oct 2024 13:44:01 +0100 Subject: [PATCH] EnableConsoleLogging --- TinyInsights/ApplicationInsightsProvider.cs | 48 ++++++++++++++------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/TinyInsights/ApplicationInsightsProvider.cs b/TinyInsights/ApplicationInsightsProvider.cs index 777b34b..da2c7a6 100644 --- a/TinyInsights/ApplicationInsightsProvider.cs +++ b/TinyInsights/ApplicationInsightsProvider.cs @@ -27,6 +27,7 @@ public class ApplicationInsightsProvider : IInsightsProvider, ILogger public bool IsAutoTrackPageViewsEnabled { get; set; } = true; public bool IsTrackEventsEnabled { get; set; } = true; public bool IsTrackDependencyEnabled { get; set; } = true; + public bool EnableConsoleLogging { get; set; } #if IOS || MACCATALYST || ANDROID @@ -160,7 +161,8 @@ private static void OnAppearing(object? sender, Page e) } catch (Exception) { - Debug.WriteLine("TinyInsights: Error creating TelemetryClient"); + if (EnableConsoleLogging) + Console.WriteLine("TinyInsights: Error creating TelemetryClient"); } return null; @@ -270,7 +272,8 @@ private async Task SendCrashes() return; } - Debug.WriteLine($"TinyInsights: Sending {crashes.Count} crashes"); + if (EnableConsoleLogging) + Console.WriteLine(($"TinyInsights: Sending {crashes.Count} crashes"); foreach (var crash in crashes) { @@ -296,7 +299,8 @@ private async Task SendCrashes() } catch (Exception ex) { - Trace.WriteLine($"TinyInsights: Error sending crashes. Message: {ex.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Error sending crashes. Message: {ex.Message}"); } } @@ -329,14 +333,16 @@ private void ResetCrashes() { try { - Debug.WriteLine("TinyInsights: Reset crashes"); + if (EnableConsoleLogging) + Console.WriteLine("TinyInsights: Reset crashes"); var path = Path.Combine(logPath, crashLogFilename); File.Delete(path); } catch (Exception ex) { - Trace.WriteLine($"TinyInsights: Error clearing crashes. Message: {ex.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Error clearing crashes. Message: {ex.Message}"); } } @@ -344,7 +350,8 @@ private void HandleCrash(Exception ex) { try { - Debug.WriteLine("TinyInsights: Handle crashes"); + if (EnableConsoleLogging) + Console.WriteLine("TinyInsights: Handle crashes"); var crashes = ReadCrashes() ?? []; @@ -358,7 +365,8 @@ private void HandleCrash(Exception ex) } catch (Exception exception) { - Trace.WriteLine($"TinyInsights: Error handling crashes. Message: {exception.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Error handling crashes. Message: {exception.Message}"); } } @@ -371,7 +379,8 @@ public async Task TrackErrorAsync(Exception ex, Dictionary? prop return; } - Debug.WriteLine($"TinyInsights: Tracking error {ex.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Tracking error {ex.Message}"); properties ??= []; @@ -385,7 +394,8 @@ public async Task TrackErrorAsync(Exception ex, Dictionary? prop } catch (Exception exception) { - Trace.WriteLine($"TinyInsights: Error tracking error. Message: {exception.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Error tracking error. Message: {exception.Message}"); } } @@ -398,14 +408,16 @@ public async Task TrackEventAsync(string eventName, Dictionary? return; } - Debug.WriteLine($"TinyInsights: Tracking event {eventName}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Tracking event {eventName}"); Client.TrackEvent(eventName, properties); await Client.FlushAsync(CancellationToken.None); } catch (Exception ex) { - Trace.WriteLine($"TinyInsights: Error tracking event. Message: {ex.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Error tracking event. Message: {ex.Message}"); } } @@ -418,14 +430,16 @@ public async Task TrackPageViewAsync(string viewName, Dictionary return; } - Debug.WriteLine($"TinyInsights: tracking page view {viewName}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: tracking page view {viewName}"); Client.TrackPageView(viewName); await Client.FlushAsync(CancellationToken.None); } catch (Exception ex) { - Trace.WriteLine($"TinyInsights: Error tracking page view. Message: {ex.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Error tracking page view. Message: {ex.Message}"); } } @@ -438,7 +452,8 @@ public async Task TrackDependencyAsync(string dependencyType, string dependencyN return; } - Debug.WriteLine($"TinyInsights: Tracking dependency {dependencyName}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Tracking dependency {dependencyName}"); var fullUrl = data; @@ -483,7 +498,8 @@ public async Task TrackDependencyAsync(string dependencyType, string dependencyN } catch (Exception ex) { - Trace.WriteLine($"TinyInsights: Error tracking dependency. Message: {ex.Message}"); + if (EnableConsoleLogging) + Console.WriteLine($"TinyInsights: Error tracking dependency. Message: {ex.Message}"); } } @@ -518,7 +534,7 @@ public async void Log(LogLevel logLevel, EventId eventId, TState state, private static Task TrackDebugAsync(EventId eventId, TState state, Exception? exception) { - Debug.WriteLine($"TinyInsights: DebugLogging, Event: {GetEventName(eventId)}, State: {state}, Exception: {exception?.Message}"); + Console.WriteLine($"TinyInsights: DebugLogging, Event: {GetEventName(eventId)}, State: {state}, Exception: {exception?.Message}"); return Task.CompletedTask; }