For .NET MAUI, go to: https://github.com/dhindrik/TinyInsights.Maui
The idea behind TinyInsights is to build a cross platform library to use with your favorite Analytics provider. Right now there are a providers for AppCenter, Application Insights and Google Analytics. And you are of course welcome to contribute with your favorite provider.
1.1 - Introducing an Application Insigts provider and a new method for tracking dependencies. Read more in this blog post, https://danielhindrikes.se/index.php/2020/03/10/application-insights-for-xamarin-and-uwp-apps-with-tinyinsights/
Install the Nuget package for the provider you want to use in the platform project, in other projects use the TinyInsights package.
For Microsoft AppCenter install the package TinyInsights.AppCenter.
Ìnstall-Package TinyInsights.AppCenter
For Azure ApplicationInsights install the package TinyInsights.ApplicationInsights.
Ìnstall-Package TinyInsights.ApplicationInsights
If you have more projects in yout solution and want to use TinyInsights in them, you can install just the TinyInsights package. The "provider package" is just needed in the procject handling the configuration.
Ìnstall-Package TinyInsights
var appCenterProvider = new AppCenterProvider(iOSKey, AndroidKey, UWPKey)
TinyInsights.Configure(appCenterProvider);
When multiple providers will be available you can use them simultaneously, just use configure for all of the providers.
var appInsightsProvider = new ApplicationInsightsProvider("{InstrumentationKey}");
TinyInsights.Configure(appCenterProvider, appInsightsProvider);
If you have multiple providers you can configure what to track with each provider. By default everything is tracked.
appCenterProvider.IsTrackPageViewsEnabled = false;
appCenterProvider.IsTrackEventsEnabled = false;
appCenterProvider.IsTrackDependencyEnabled = false;
appInsightsProvider.IsTrackErrorsEnabled = false;
catch(Ecception ex)
{
await TinyInsights.TrackErrorAsync(ex);
}
//with properties
var properties = new Dictionarty<string, string>();
properties.Add("MyFirstProperty", "MyFirstValue");
properties.Add("MySecondProperty", "MySeconndValue");
catch(Ecception ex)
{
await TinyInsights.TrackErrorAsync(ex, properties);
}
await TinyInsights.TrackPageViewAsync("SuperCoolView");
//with properties
var properties = new Dictionarty<string, string>();
properties.Add("MyFirstProperty", "MyFirstValue");
properties.Add("MySecondProperty", "MySeconndValue");
await TinyInsights.TrackPageViewAsync("SuperCoolView", properties);
await TinyInsights.TrackEventAsync("SuperCoolEvent");
//with properties
var properties = new Dictionarty<string, string>();
properties.Add("MyFirstProperty", "MyFirstValue");
properties.Add("MySecondProperty", "MySeconndValue");
await TinyInsights.TrackEventAsync("SuperCoolEvent", properties);
There are a two of ways to track dependencies with TinyInsights. The first and the basic method is TrackDependencyAsync, and is also used in the background by the other way to do it.
var startTime = DateTimeOffset.Now;
var success = await GetData();
var duration = DateTimeOffset.Now - startTime
await TinyInsights.TrackDependencyAsync("api.mydomain.se", "https://api/mydomain.se/v1/data/get", startTime, duration, success);
The second way is to create a TinyDependency object that handles most of the tracking for you. You will do that by just by wrapping your code for the dependency in a using statement.
using (var tracker = TinyInsights.CreateDependencyTracker("api.mydomain.se", "https://api/mydomain.se/v1/data/get"))
{
await GetData();
}
If the dependency succeded that is fine, but if it not you need to handle that on your own, using the Finish method of the TinyDependency object.
using (var tracker = TinyInsights.CreateDependencyTracker("api.mydomain.se", "https://api/mydomain.se/v1/data/get"))
{
try
{
var repsonse = await GetData();
if(!response.IsSuccessStatusCode)
{
await tracker.Finish(false, (int)response.StatusCode);
}
}
catch(Exception ex)
{
tracker.Finish(false, ex);
}
}