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

Manually handle sending of crashes #43

Merged
merged 15 commits into from
Dec 18, 2024
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
99 changes: 82 additions & 17 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class ApplicationInsightsProvider : IInsightsProvider, ILogger
{
public 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

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);

private const string crashLogFilename = "crashes.mauiinsights";
Expand All @@ -23,6 +23,7 @@

public bool IsTrackErrorsEnabled { get; set; } = true;
public bool IsTrackCrashesEnabled { get; set; } = true;
public bool WriteCrashes { get; set; } = true;
public bool IsTrackPageViewsEnabled { get; set; } = true;
public bool IsAutoTrackPageViewsEnabled { get; set; } = true;
public bool IsTrackEventsEnabled { get; set; } = true;
Expand Down Expand Up @@ -106,7 +107,10 @@
Application.Current.PageDisappearing += weakOnDisappearingHandler.Handler;
}

Task.Run(SendCrashes);
if (WriteCrashes)
{
Task.Run(SendCrashes);
}

IsInitialized = true;
}
Expand Down Expand Up @@ -163,7 +167,36 @@
// Add any global properties, the user has already added
foreach (var property in _globalProperties)
{
client.Context.GlobalProperties[property.Key] = property.Value;
switch(property.Key)
{
case "Cloud.RoleName":
client.Context.Cloud.RoleName = property.Value;
break;

case "Cloud.RoleInstance":
client.Context.Cloud.RoleInstance = property.Value;
break;

case "Device.OperatingSystem":
client.Context.Device.OperatingSystem = property.Value;
break;

case "Device.Model":
client.Context.Device.Model = property.Value;
break;

case "Device.Type":
client.Context.Device.Type = property.Value;
break;

case "Device.Id":
client.Context.Device.Id = property.Value;
break;

default:
client.Context.GlobalProperties[property.Key] = property.Value;
break;
}
}

client.Context.GlobalProperties.TryAdd("Language", CultureInfo.CurrentUICulture.TwoLetterISOLanguageName);
Expand All @@ -173,7 +206,6 @@
client.Context.GlobalProperties.TryAdd("OperatingSystemVersion", DeviceInfo.VersionString);
client.Context.Session.Id = Guid.NewGuid().ToString();


return client;
}
catch (Exception)
Expand All @@ -187,7 +219,9 @@

public void UpsertGlobalProperty(string key, string value)
{
if (Client is null)
_globalProperties[key] = value;

if(Client is null)
{
return;
}
Expand All @@ -196,29 +230,32 @@
{
case "Cloud.RoleName":
Client.Context.Cloud.RoleName = value;
return;
break;

case "Cloud.RoleInstance":
Client.Context.Cloud.RoleInstance = value;
return;
break;

case "Device.OperatingSystem":
Client.Context.Device.OperatingSystem = value;
return;
break;

case "Device.Model":
Client.Context.Device.Model = value;
return;
break;

case "Device.Type":
Client.Context.Device.Type = value;
return;
break;

case "Device.Id":
Client.Context.Device.Id = value;
return;
}

_globalProperties[key] = value;

Client.Context.GlobalProperties[key] = value;
break;

default:
Client.Context.GlobalProperties[key] = value;
break;
}
}

public void RemoveGlobalProperty(string key)
Expand Down Expand Up @@ -273,7 +310,7 @@
Client.Context.Session.Id = Guid.NewGuid().ToString();
}

private async Task SendCrashes()
public async Task SendCrashes()
{
try
{
Expand Down Expand Up @@ -321,6 +358,29 @@
}
}

public bool HasCrashed()
{
try
{
var path = Path.Combine(logPath, crashLogFilename);

if(!File.Exists(path))
{
return false;
}

var json = File.ReadAllText(path);

var crashes = string.IsNullOrWhiteSpace(json) ? null : JsonSerializer.Deserialize<List<Crash>>(json);

return crashes is null ? false : crashes.Count != 0;
}
catch(Exception)
{
return false;
}
}

private List<Crash>? ReadCrashes()
{
try
Expand All @@ -346,7 +406,7 @@
return null;
}

private void ResetCrashes()
public void ResetCrashes()
{
try
{
Expand Down Expand Up @@ -447,6 +507,11 @@
return;
}

if (!IsTrackPageViewsEnabled)
{
return;
}

if (EnableConsoleLogging)
Console.WriteLine($"TinyInsights: tracking page view {viewName}");

Expand Down
10 changes: 10 additions & 0 deletions TinyInsights/IInsights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ namespace TinyInsights;
public interface IInsights
{
void AddProvider(IInsightsProvider provider);

IReadOnlyList<IInsightsProvider> GetProviders();

void UpsertGlobalProperty(string key, string value);

void RemoveGlobalProperty(string key);

Task TrackErrorAsync(Exception ex, Dictionary<string, string>? properties = null);

Task TrackPageViewAsync(string viewName, Dictionary<string, string>? properties = null, TimeSpan? duration = null);

Task TrackEventAsync(string eventName, Dictionary<string, string>? properties = null);

Task TrackErrorAsync(Exception ex, ErrorSeverity severity, Dictionary<string, string>? properties = null);

Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null);
Expand All @@ -25,5 +28,12 @@ Task TrackDependencyAsync(string dependencyType, string dependencyName, string d
void OverrideAnonymousUserId(string userId);

void GenerateNewAnonymousUserId();

void CreateNewSession();

bool HasCrashed();

Task SendCrashes();

void ResetCrashes();
}
11 changes: 10 additions & 1 deletion TinyInsights/IInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ namespace TinyInsights;
public interface IInsightsProvider
{
bool IsTrackErrorsEnabled { get; set; }
public bool IsTrackCrashesEnabled { get; set; }
bool IsTrackCrashesEnabled { get; set; }
bool WriteCrashes { get; set; }
bool IsTrackPageViewsEnabled { get; set; }
bool IsAutoTrackPageViewsEnabled { get; set; }
bool IsTrackEventsEnabled { get; set; }
Expand All @@ -14,6 +15,7 @@ public interface IInsightsProvider
void Initialize();

void UpsertGlobalProperty(string key, string value);

void RemoveGlobalProperty(string key);

Task TrackErrorAsync(Exception ex, Dictionary<string, string>? properties = null);
Expand All @@ -29,5 +31,12 @@ public interface IInsightsProvider
void OverrideAnonymousUserId(string userId);

string GenerateNewAnonymousUserId();

void CreateNewSession();

bool HasCrashed();

Task SendCrashes();

void ResetCrashes();
}
30 changes: 30 additions & 0 deletions TinyInsights/Insights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,34 @@ public void CreateNewSession()
provider.CreateNewSession();
}
}

public bool HasCrashed()
{
foreach(var provider in insightsProviders)
{
bool hasCrashed = provider.HasCrashed();
if(hasCrashed)
{
return true;
}
}

return false;
}

public async Task SendCrashes()
{
foreach(var provider in insightsProviders)
{
await provider.SendCrashes();
}
}

public void ResetCrashes()
{
foreach(var provider in insightsProviders)
{
provider.ResetCrashes();
}
}
}
Loading