diff --git a/TinyInsights/ApplicationInsightsProvider.cs b/TinyInsights/ApplicationInsightsProvider.cs index b3989a1..2a5f174 100644 --- a/TinyInsights/ApplicationInsightsProvider.cs +++ b/TinyInsights/ApplicationInsightsProvider.cs @@ -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; @@ -106,7 +107,10 @@ public void Initialize() Application.Current.PageDisappearing += weakOnDisappearingHandler.Handler; } - Task.Run(SendCrashes); + if (WriteCrashes) + { + Task.Run(SendCrashes); + } IsInitialized = true; } @@ -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); @@ -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) @@ -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; } @@ -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) @@ -273,7 +310,7 @@ public void CreateNewSession() Client.Context.Session.Id = Guid.NewGuid().ToString(); } - private async Task SendCrashes() + public async Task SendCrashes() { try { @@ -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>(json); + + return crashes is null ? false : crashes.Count != 0; + } + catch(Exception) + { + return false; + } + } + private List? ReadCrashes() { try @@ -346,7 +406,7 @@ private async Task SendCrashes() return null; } - private void ResetCrashes() + public void ResetCrashes() { try { @@ -447,6 +507,11 @@ public async Task TrackPageViewAsync(string viewName, Dictionary return; } + if (!IsTrackPageViewsEnabled) + { + return; + } + if (EnableConsoleLogging) Console.WriteLine($"TinyInsights: tracking page view {viewName}"); diff --git a/TinyInsights/IInsights.cs b/TinyInsights/IInsights.cs index d024d4f..5060ed4 100644 --- a/TinyInsights/IInsights.cs +++ b/TinyInsights/IInsights.cs @@ -3,9 +3,11 @@ namespace TinyInsights; public interface IInsights { void AddProvider(IInsightsProvider provider); + IReadOnlyList GetProviders(); void UpsertGlobalProperty(string key, string value); + void RemoveGlobalProperty(string key); Task TrackErrorAsync(Exception ex, Dictionary? properties = null); @@ -13,6 +15,7 @@ public interface IInsights Task TrackPageViewAsync(string viewName, Dictionary? properties = null, TimeSpan? duration = null); Task TrackEventAsync(string eventName, Dictionary? properties = null); + Task TrackErrorAsync(Exception ex, ErrorSeverity severity, Dictionary? properties = null); Task TrackDependencyAsync(string dependencyType, string dependencyName, string data, DateTimeOffset startTime, TimeSpan duration, bool success, int resultCode = 0, Exception? exception = null); @@ -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(); } \ No newline at end of file diff --git a/TinyInsights/IInsightsProvider.cs b/TinyInsights/IInsightsProvider.cs index 7e8d1e3..9ce42cd 100644 --- a/TinyInsights/IInsightsProvider.cs +++ b/TinyInsights/IInsightsProvider.cs @@ -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; } @@ -14,6 +15,7 @@ public interface IInsightsProvider void Initialize(); void UpsertGlobalProperty(string key, string value); + void RemoveGlobalProperty(string key); Task TrackErrorAsync(Exception ex, Dictionary? properties = null); @@ -29,5 +31,12 @@ public interface IInsightsProvider void OverrideAnonymousUserId(string userId); string GenerateNewAnonymousUserId(); + void CreateNewSession(); + + bool HasCrashed(); + + Task SendCrashes(); + + void ResetCrashes(); } \ No newline at end of file diff --git a/TinyInsights/Insights.cs b/TinyInsights/Insights.cs index 22e2c20..32da46e 100644 --- a/TinyInsights/Insights.cs +++ b/TinyInsights/Insights.cs @@ -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(); + } + } } \ No newline at end of file