Thomas
Thomas

Reputation: 3

Not receiving Application Insights Telemetry from an Android app in Release configuration

I've spent the better part of 2 days on the following issue and I'm running out of ideas. Hopefully someone has had a similar experience and can help me out.

I have a .NET 8.0 iOS, Windows, and Android app with a shared backend. Exception logging to application insights is working perfectly for iOS and Windows, but it only works for Android in the DEBUG configuration of my app.

Reproduction

Instantiating the telemetry client during app start:

var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
    var telemetryConnection = config["ApplicationInsightsConnection"];
    telemetryConfiguration.ConnectionString = telemetryConnection;

    var telemetryClient = new TelemetryClient(telemetryConfiguration);
    Mvx.IoCProvider.RegisterSingleton(telemetryClient);

Writing an Event or Exception to Application Insights:

    /// <inheritdoc />
    public void LogException(Exception exception, string? message, IDictionary<string, string>  properties)
    {
        var telemetryClient = Mvx.IoCProvider?.Resolve<TelemetryClient>();
        
        // Both Don't work
        telemetryClient?.TrackException(new ExceptionTelemetry(new Exception("Test Thomas Release Config")));
        telemetryClient?.TrackEvent("Test Thomas Release Config");
    }

Expected behavior

On Android, after compiling my app in Release Mode, I expect exceptions that I send with telemetryClient.Track to be visible in Application insights.

Relevant package, tooling and runtime versions

Application insights version 2.22.0 Platform: .Net 8 app, Android (no Maui).

Additional Context

Upvotes: 0

Views: 274

Answers (2)

Joost Jens
Joost Jens

Reputation: 86

The answer to this was actually very simple. The "Internet" permission is required. It turns out this gets added automatically by the ApplicationCenter packages, but not by the Application Insights packages.

<uses-permission android:name="android.permission.INTERNET" />

Upvotes: 0

Suresh Chikkam
Suresh Chikkam

Reputation: 3413

  • If you are using Proguard or R8 for code shrinking, Include rules to keep the Application Insights libraries.

proguard-rules.pro:

-keep class com.microsoft.** { *; }
-keep class io.opentelemetry.** { *; }
-keep class com.azure.** { *; }
-keep class com.microsoft.applicationinsights.** { *; }

Here, I have installed below dependencies for the application.

enter image description here

Release mode configurations are different from Debug mode configurations. Check that you are flushing telemetry data before the app shuts down or crashes. This is especially important in Release mode where optimizations might cause the app to terminate more aggressively.

App.xaml.cs:

using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.Extensibility;

namespace MauiApp1
{
    public partial class App : Application
    {
        private static TelemetryClient _telemetryClient;

        public App()
        {
            InitializeComponent();

            // Initialize Application Insights
            var telemetryConfiguration = TelemetryConfiguration.CreateDefault();
            telemetryConfiguration.ConnectionString = "InstrumentationKey=1dc7bxx8-xxxx-487e-xxxx-af89xxxxxxa1;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/;ApplicationId=5fxxxxx5-xxxx41b6-axxx-9xxxx4xxxx1e"; // Replace with your actual Application Insights connection string
            _telemetryClient = new TelemetryClient(telemetryConfiguration);

            MainPage = new AppShell();

            // Log an exception on app start for testing
            LogException(new Exception("Test exception on app start"));
        }

        public static void LogException(Exception exception)
        {
            _telemetryClient?.TrackException(exception);
            _telemetryClient?.Flush(); // Ensure telemetry is sent immediately
        }

        public static void LogEvent(string eventName)
        {
            _telemetryClient?.TrackEvent(eventName);
            _telemetryClient?.Flush(); // Ensure telemetry is sent immediately
        }
    }
}

MainPage.xaml.cs:

using System;
using Microsoft.ApplicationInsights;

namespace MauiApp1
{
    public partial class MainPage : ContentPage
    {
        int count = 0;

        public MainPage()
        {
            InitializeComponent();
        }

        private void OnCounterClicked(object sender, EventArgs e)
        {
            count++;

            if (count == 1)
                CounterBtn.Text = $"Clicked {count} time";
            else
                CounterBtn.Text = $"Clicked {count} times";

            SemanticScreenReader.Announce(CounterBtn.Text);

            // Log a custom event for button clicks
            App.LogEvent("ButtonClicked");
        }
    }
}

enter image description here

Log Traces:

enter image description here

Upvotes: 0

Related Questions