Azure / azure-functions-dotnet-worker

Azure Functions out-of-process .NET language worker
MIT License
412 stars 176 forks source link

AI logs are not flushed when process exits #2379

Open dmytro-gokun opened 5 months ago

dmytro-gokun commented 5 months ago

Description

We have a .NET 8 function app (isolated). When the app starts we register this lifetime callbacks:

    public static IHost InitializeLogging(this IHost @this)
    {
        var logger = @this.Services.GetRequiredService<ILogger<ApplicationLog>>();
        var lifetime = @this.Services.GetRequiredService<IHostApplicationLifetime>();
        lifetime.ApplicationStarted.Register(() => logger.LogInformation("Application started"));
        lifetime.ApplicationStopping.Register(() => logger.LogInformation("Application stopping"));
        lifetime.ApplicationStopped.Register(() => logger.LogInformation("Application stopped"));
        return @this;
    }

In this application we also have configured Application Insights logging using services.AddApplicationInsightsTelemetryWorkerService()/services.ConfigureFunctionsApplicationInsights().

All in all, logging works just fine and we get all our log in AI. "Application started" is getting to AI as well. But we never get "Application stopping"/"Application stopped" in AI. My assumption is that the function app exits before those message are actually transmitted to AI server. Is this correct? Is there any way to fix this behavior?

Steps to reproduce

  1. Create an Azure Function app.
  2. Add Application insights logging.
  3. Log lifetime events
  4. Deploy Function app to Azure and run it.
  5. Make sure its stopped.
  6. Examine AI logs.
dmytro-gokun commented 4 months ago

@liliankasem I've done some additional testing and i can confirm that ApplicationStopping & ApplicationStopped events are fired indeed. So the problem (most likely) is in some events not reaching AI indeed. I think, we need some fix for this

dmytro-gokun commented 2 months ago

Is there any chance this gets worked on any time soon?