Skip to content

Commit

Permalink
Merge pull request #43 from IeuanWalker/DynamicSettings
Browse files Browse the repository at this point in the history
Manually handle sending of crashes
  • Loading branch information
dhindrik authored Dec 18, 2024
2 parents 4dd62b0 + 8e017ca commit 968c49d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 18 deletions.
99 changes: 82 additions & 17 deletions TinyInsights/ApplicationInsightsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class ApplicationInsightsProvider : IInsightsProvider, ILogger

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 @@ public void Initialize()
Application.Current.PageDisappearing += weakOnDisappearingHandler.Handler;
}

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

IsInitialized = true;
}
Expand Down Expand Up @@ -163,7 +167,36 @@ private static void OnDisappearing(object? sender, Page e)
// 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 @@ private static void OnDisappearing(object? sender, Page e)
client.Context.GlobalProperties.TryAdd("OperatingSystemVersion", DeviceInfo.VersionString);
client.Context.Session.Id = Guid.NewGuid().ToString();


return client;
}
catch (Exception)
Expand All @@ -187,7 +219,9 @@ private static void OnDisappearing(object? sender, Page e)

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 @@ public void UpsertGlobalProperty(string key, string value)
{
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 @@ public void CreateNewSession()
Client.Context.Session.Id = Guid.NewGuid().ToString();
}

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

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 @@ private async Task SendCrashes()
return null;
}

private void ResetCrashes()
public void ResetCrashes()
{
try
{
Expand Down Expand Up @@ -447,6 +507,11 @@ public async Task TrackPageViewAsync(string viewName, Dictionary<string, string>
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();
}
}
}

0 comments on commit 968c49d

Please sign in to comment.