Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exceptions are not logged to application insights for an Android app in Release configuration #226

Open
Thomas-Slippens opened this issue Aug 28, 2024 · 1 comment
Labels

Comments

@Thomas-Slippens
Copy link

Thomas-Slippens commented Aug 28, 2024

Description

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. Serilog 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"];

    if (string.IsNullOrEmpty(telemetryConnection))
    {
        throw new InvalidOperationException("The required configuration value 'ApplicationInsightsConnection' was not configured in 
        appsettings.json");
    }

    telemetryConfiguration.ConnectionString = telemetryConnection;

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

Setting up the serilogger configuration:

    Log.Logger = new LoggerConfiguration()
        .MinimumLevel.Debug()
        .WriteTo.Async(a => a.AndroidLog())
        .WriteTo.ApplicationInsights(iocProvider.Resolve<TelemetryClient>(), 
            TelemetryConverter.Events).CreateLogger();

Writing an exception (where _platformSpecificLogger is an instance of SeriLog implementing ILogger)

_platformSpecificLogger?.LogError(exception, $"{message}, {toAppend}", toAppend);

ONLY In case the exception is unhandled we finalize with this in de Android project:

 private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception exception = e.ExceptionObject as Exception ?? new Exception($"{nameof(UnhandledExceptionEventArgs)}. 
   {nameof(e.ExceptionObject)} is not of type Exception; {e.ExceptionObject}");

    HandleException(exception);

    // Makes sure serilog sends application insights telemetry before the application terminates.
    **Serilog.Log.CloseAndFlush();**
} 

Expected behavior

On Android, after compiling my app in Release Mode, I expect exceptions logged by Serilog to be visible in Application insights.

Relevant package, tooling and runtime versions

The relevant setup:
Serilog version 4.0.1
Serilog app insights version 4.0.0
Application insights version 2.22.0
Platform: .Net 8 app, Android (no Maui).

Additional Context

  1. Serilog throws no exceptions and all the logger, telemetryClient, connection string etc. are all properly instantiated and have correct values.
  2. Turning off code optimalization options in .csproj (like linking) in Release mode makes no difference.
  3. Directly sending Telemetry via the telemetry client instance DOES work, also in release mode.
  4. I want to stretch that I do exactly the same thing for the Windos and iOS apps. In those cases, the Exceptions do arrive in application insights. Also in release mode.
  5. Size of the exception makes no difference. It doesn't arrive in application insights even if I do: throw new Exception("test").
@Thomas-Slippens Thomas-Slippens changed the title Exception are not logged to application insights for an Android app in Release configuration Exceptions are not logged to application insights for an Android app in Release configuration Aug 28, 2024
@nblumhardt
Copy link
Contributor

Hi @Thomas-Slippens - unfortunately only a very limited number of maintainers watch this repository, and it doesn't look like anyone immediately spotted the problem here. Posting this to Stack Overflow with the serilog tag should get more eyes on it. Best of luck!

@nblumhardt nblumhardt added question and removed bug labels Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants