Search code examples
c#androidazure-application-insightsmvvmcross

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

  • The telemetryClient throws no exceptions and the telemetryClient, connection strings etc. are all properly instantiated and have correct values and types when I log them to the Android console.
  • The telemetry client also doesn't send the telemetry if I handle the exception (and thus the app doesn't crash).
  • Turning off code optimalization options in .csproj (like linking) in Release mode makes no difference.
  • Sending event telemetry via the telemetry client instance instead of error telemetry ***also *** doesn't work (release config only).
  • Size of the exception makes no difference. It doesn't arrive in application insights even if I do: throw new Exception("test") for example.
  • I want to stretch that I do exactly the same thing for the Windows and iOS apps that share the same domain code. In those cases, the Exceptions do arrive in application insights. Also in release config.

Solution

  • 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" />