microsoft / ApplicationInsights-dotnet

ApplicationInsights-dotnet
MIT License
566 stars 285 forks source link

Microsoft.ApplicationInsights.WorkerService does not log traces even after specifying override in config #2849

Open karun-verghese opened 6 months ago

karun-verghese commented 6 months ago

Dear team,

I have a .net core Worker service for which I had configured App Insights using the below snippet in my ConfigureServices method:

 services.AddLogging(builder =>
 {
     builder.AddConsole();
     builder.AddDebug();
     builder.AddApplicationInsightsWebJobs(options =>
     {
         options.ConnectionString = configuration.GetConnectionString("ApplicationInsights");
         options.EnableLiveMetrics = true;
     });
 });

Using this (the above snippet uses the Microsoft.Azure.WebJobs.Logging.ApplicationInsights package which I know is related to another repo, but bear with me) I was only able to see traces and exceptions in my logs. I wanted to setup telemetry to track my dependency calls as well. So after reading up, I tried to use and configure the ApplicationInsights.WorkerService library instead of the above package. So my new setup looked like below:

   var aiOptions = new ApplicationInsightsServiceOptions
   {
       EnableAdaptiveSampling = false,
       ConnectionString = configuration.GetConnectionString("ApplicationInsights")
   };

   services.AddApplicationInsightsTelemetryWorkerService(aiOptions);

My dependency telemetry started showing up, but now my traces disappeared.

Following your documentation here, I provided a specific override for ApplicationInsights in the configuration(with the "Information" level enabled) , but this did not work either. I still only saw the dependencies in my logs.

The only way I got both my traces and dependencies working is to use both the packages and configure them very specifically in the following order:

 services.AddApplicationInsightsTelemetryWorkerService(aiOptions); //This has to go first
services.AddLogging(builder =>
 {
     builder.AddConsole();
     builder.AddDebug();
     builder.AddApplicationInsightsWebJobs(options =>
     {
         options.ConnectionString = configuration.GetConnectionString("ApplicationInsights");
         options.EnableLiveMetrics = true;
     });
 });

Can you please check why the logging of traces does not work? I would expect that only the WorkerService SDK is required since it has all the providers required according to the documentation.

Further information on my setup: .Net Core 8.0 Project SDK used -> Microsoft.NET.Sdk

cijothomas commented 6 months ago

https://github.com/microsoft/ApplicationInsights-dotnet/tree/develop/examples/WorkerService There is an example shared in this repo. Is that no working? i.e is that not capturing Logs?

karun-verghese commented 6 months ago

@cijothomas In the example you shared, do you mean the use of some CustomTelemetryProcessor? Other than that, the example looks pretty similar to what I had done above.

One question, does the fact that we are not using the Worker SDK but only the .Net SDK make a difference? That's something I did not try.

cijothomas commented 6 months ago

In the example you shared, do you mean the use of some CustomTelemetryProcessor?

No. telemetry processor is just shown for example, and has no impact to telemetry pipeline.

Have you confirmed if the shared example is working or not?