serilog-contrib / serilog-sinks-applicationinsights

A Serilog sink that writes events to Microsoft Azure Application Insights
Apache License 2.0
220 stars 72 forks source link

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

Open Thomas-Slippens opened 2 months ago

Thomas-Slippens commented 2 months ago

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").
nblumhardt commented 2 months ago

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!