Shaunebu.Analytics.AppInsights
1.0.3
dotnet add package Shaunebu.Analytics.AppInsights --version 1.0.3
NuGet\Install-Package Shaunebu.Analytics.AppInsights -Version 1.0.3
<PackageReference Include="Shaunebu.Analytics.AppInsights" Version="1.0.3" />
<PackageVersion Include="Shaunebu.Analytics.AppInsights" Version="1.0.3" />
<PackageReference Include="Shaunebu.Analytics.AppInsights" />
paket add Shaunebu.Analytics.AppInsights --version 1.0.3
#r "nuget: Shaunebu.Analytics.AppInsights, 1.0.3"
#:package Shaunebu.Analytics.AppInsights@1.0.3
#addin nuget:?package=Shaunebu.Analytics.AppInsights&version=1.0.3
#tool nuget:?package=Shaunebu.Analytics.AppInsights&version=1.0.3
Shaunebu.Analytics.AppInsights 📗
A lightweight, cross-platform application lifecycle manager for .NET MAUI. This library provides foreground/background lifecycle events across Android, iOS, and Windows, allowing developers to react to app state changes in a consistent way. It abstracts platform differences and exposes a simple event-driven API.
Features
Singleton-based AppInsights telemetry manager for MAUI + .NET Standard.
Tracks custom events, page views, and exceptions.
Supports offline buffering and batch flushing.
Automatically enriches telemetry with platform, OS, app version, session, and user info.
Lifecycle-aware: automatically flushes on app resume or sleep.
Global default properties for all telemetry.
Fully compatible with Microsoft.ApplicationInsights TelemetryClient.
Optional debug mode for local logging.
Installation
Install via NuGet:
Install-Package Shaunebu.Analytics.AppInsights
Add namespace references in your project:
using Shaunebu.Analytics.AppInsights;
Initialization
// Initialize AppInsightsManager in App.xaml.cs
AppInsightsManager.Current.Initialize(
connectionString: "<YOUR-CONNECTION-STRING>",
debugMode: true, // Optional: enables debug logging
batchIntervalSeconds: 30 // Optional: flush interval in seconds
);
Optional: provide a custom TelemetryClient:
var customClient = new TelemetryClient(new TelemetryConfiguration("<YOUR-CONNECTION-STRING>"));
AppInsightsManager.Current.Initialize("<YOUR-CONNECTION-STRING>", customClient: customClient);
Usage
Track Custom Event
AppInsightsManager.Current.TrackEvent(
"ButtonClicked",
new Dictionary<string, string>
{
{ "ButtonName", "Login" }
},
metric: 1
);
Track Page View
AppInsightsManager.Current.TrackPageView("MainPage");
Track Exception
try
{
// Some code that throws
}
catch (Exception ex)
{
AppInsightsManager.Current.TrackException(ex, new Dictionary<string, string>
{
{ "Context", "LoginButtonClicked" }
});
}
Add Global Properties
AppInsightsManager.Current.AddGlobalProperty("AppFlavor", "Beta");
Flush Telemetry Immediately
await AppInsightsManager.Current.FlushAsync();
Properties
| Property | Type | Description |
|---|---|---|
Current |
AppInsightsManager |
Singleton instance of the manager. |
connectionString |
string? |
AppInsights connection string. |
Logger |
IAppLogger? |
Optional logger for debug/info/error messages. |
EnableDebugMode |
bool |
Enable or disable debug logging. |
SessionId |
string |
Current session identifier (auto-generated if not set). |
UserId |
string |
Current user identifier (persisted via Preferences). |
Methods
| Method | Parameters | Description |
|---|---|---|
Initialize(string connectionString, bool debugMode = false, int batchIntervalSeconds = 30, TelemetryClient? customClient = null) |
instrumentationKey: AppInsights key <br>debugMode: enable debug logging <br>batchIntervalSeconds: flush interval <br>customClient: optional custom TelemetryClient |
Initializes the telemetry manager and hooks into MAUI lifecycle events. |
TrackEvent(string name, Dictionary<string, string>? properties = null, double? metric = null) |
name: event name <br>properties: optional properties <br>metric: optional numeric value |
Tracks a custom event. |
TrackPageView(string pageName, Dictionary<string, string>? properties = null) |
pageName: page identifier <br>properties: optional properties |
Tracks a page view. |
TrackException(Exception ex, Dictionary<string, string>? properties = null) |
ex: Exception object <br>properties: optional properties |
Tracks an exception. |
AddGlobalProperty(string key, string value) |
key: property name <br>value: property value |
Adds a property to all future telemetry items. |
FlushAsync() |
None | Flushes all buffered telemetry immediately. |
Example: Full App.xaml.cs
public partial class App : Application
{
public App()
{
InitializeComponent();
// Initialize AppInsights
AppInsightsManager.Current.Logger = new ConsoleLogger();
AppInsightsManager.Current.Initialize("<YOUR-CONNECTION-STRING>", debugMode: true);
// Track app start
AppInsightsManager.Current.TrackEvent("App_Started");
}
}
Logger Interface
Implement your own logger if needed:
public class ConsoleLogger : AppInsightsManager.IAppLogger
{
public void Info(string message) => Console.WriteLine("[INFO] " + message);
public void Warn(string message) => Console.WriteLine("[WARN] " + message);
public void Error(string message, Exception? ex = null)
{
Console.WriteLine("[ERROR] " + message + (ex != null ? " " + ex.Message : ""));
}
}
Lifecycle & Platform Awareness
Hooks into Application.Current.Resumed and Application.Current.Slept to automatically flush telemetry.
Automatically enriches all telemetry with:
Platform(Android/iOS/Windows)OSVersionAppVersionAppBuildSessionIdUserId
Notes & Best Practices
Enable DebugMode for development to log telemetry locally.
AddGlobalProperty for app-wide telemetry metadata.
FlushAsync should be called during app shutdown if needed.
Works with .NET MAUI and .NET Standard 2.1+ libraries.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Microsoft.ApplicationInsights (>= 2.21.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Shaunebu.Analytics.AppInsights:
| Package | Downloads |
|---|---|
|
Shaunebu.Bussiness.ReportGenerator
Enterprise-grade modular reporting SDK for .NET — async, pluggable, and secure. Generate reports (PDF, Excel, Word, HTML, Markdown) with templating, telemetry, and compliance. |
GitHub repositories
This package is not used by any popular GitHub repositories.