microsoft / ApplicationInsights-dotnet-logging

.NET Logging adaptors
106 stars 49 forks source link

Telemetry Client is not flushed on console exit #276

Closed nhart12 closed 5 years ago

nhart12 commented 5 years ago

With a console application on dotnet core 2.2 I'm finding not all events are making it to AI. Here is a simple repro project where log is not sent :

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
     <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.9.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
  </ItemGroup>
</Project>

And the Program.cs (Note I'm even disposing of the ServiceProvider.)

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace ConsoleApp1
{
    class Program
    {

    static void Main(string[] args)
    {
        IServiceCollection services = new ServiceCollection();

        // Add the logging pipelines to use. We are using Application Insights only here.
        services.AddLogging(loggingBuilder =>
        {
            loggingBuilder.AddApplicationInsights("my-key");
            loggingBuilder.AddConsole();
        });

        // Build ServiceProvider.
        using (var serviceProvider = services.BuildServiceProvider())
        {
            ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();

            logger.LogInformation("Logger is working");
        }
    }
    }
}

If I fork this library and add a telemetryClient.Flush() then my log is sent (only if I dispose the ServiceProvider). Line 108 here: https://github.com/Microsoft/ApplicationInsights-dotnet-logging/blob/develop/src/ILogger/ApplicationInsightsLoggerProvider.cs


         /// Releases unmanaged and - optionally - managed resources. 
         /// </summary>
         /// <param name="releasedManagedResources">Release managed resources.</param>
         protected virtual void Dispose(bool releasedManagedResources)
         {
             this.telemetryClient.Flush(); //Line 108 on ApplicationInsightsLoggerProvider.cs
         }
cijothomas commented 5 years ago

Thanks for reporting! Sorry for the bad state of docs.

You are right - flush() is not done automatically. Please see the workaround here: https://stackoverflow.com/questions/55289534/why-is-ilogger-not-logging-to-azure-app-insights/55290922#55290922

cijothomas commented 5 years ago

docs are being updated to use new example with flush. Once that is done, we'll fix the product itself.

nhart12 commented 5 years ago

@cijothomas this workaround seems to stop working in later releases of Microsoft.ApplicationInisghts 2.10 +

cijothomas commented 5 years ago

@nhart12 Did you mean 2.10.0-beta4 which was just released? I'll check this, but if you have more information, please do share.

nhart12 commented 5 years ago

@cijothomas actually I was wrong, I believe it's the update from Microsoft.Applications.AspNetCore from 2.6.1 > 2.7.0-beta3 that made the logging stop working. I'm still digging into it on my end.

nhart12 commented 5 years ago

Actually after some digging, I believe in 2.7 I no longer need the loggingBuilder.AddApplicationInsights("my-key"); call and it is default filtering my information logs. I think I have it working again. Do you know if the flush is still required in 2.7-beta3? Edit: Yes it's still needed

cijothomas commented 5 years ago

Ok. in 2.7.0-beta3 onwards, ilogger is automatically captured.

Flush is only needed for console apps where app is exiting, and there are still items pending in buffer..

cijothomas commented 5 years ago

Closing in favor of https://github.com/microsoft/ApplicationInsights-dotnet-logging/issues/284