Skip to content

Commit

Permalink
EnableConsoleLogging
Browse files Browse the repository at this point in the history
  • Loading branch information
dhindrik committed Oct 31, 2024
1 parent 4c3ed62 commit ccbc9b9
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

Check failure on line 276 in TinyInsights/ApplicationInsightsProvider.cs

View workflow job for this annotation

GitHub Actions / build

) expected

foreach (var crash in crashes)
{
Expand All @@ -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}");
}
}

Expand Down Expand Up @@ -329,22 +333,25 @@ 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}");
}
}

private void HandleCrash(Exception ex)
{
try
{
Debug.WriteLine("TinyInsights: Handle crashes");
if (EnableConsoleLogging)
Console.WriteLine("TinyInsights: Handle crashes");

var crashes = ReadCrashes() ?? [];

Expand All @@ -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}");
}
}

Expand All @@ -371,7 +379,8 @@ public async Task TrackErrorAsync(Exception ex, Dictionary<string, string>? prop
return;
}

Debug.WriteLine($"TinyInsights: Tracking error {ex.Message}");
if (EnableConsoleLogging)
Console.WriteLine($"TinyInsights: Tracking error {ex.Message}");

properties ??= [];

Expand All @@ -385,7 +394,8 @@ public async Task TrackErrorAsync(Exception ex, Dictionary<string, string>? prop
}
catch (Exception exception)
{
Trace.WriteLine($"TinyInsights: Error tracking error. Message: {exception.Message}");
if (EnableConsoleLogging)
Console.WriteLine($"TinyInsights: Error tracking error. Message: {exception.Message}");
}
}

Expand All @@ -398,14 +408,16 @@ public async Task TrackEventAsync(string eventName, Dictionary<string, string>?
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}");
}
}

Expand All @@ -418,14 +430,16 @@ public async Task TrackPageViewAsync(string viewName, Dictionary<string, string>
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}");
}
}

Expand All @@ -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;

Expand Down Expand Up @@ -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}");
}
}

Expand Down Expand Up @@ -518,7 +534,7 @@ public async void Log<TState>(LogLevel logLevel, EventId eventId, TState state,

private static Task TrackDebugAsync<TState>(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;
}

Expand Down

0 comments on commit ccbc9b9

Please sign in to comment.