ApplicationInsights-dotnet icon indicating copy to clipboard operation
ApplicationInsights-dotnet copied to clipboard

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

Open karun-verghese opened this issue 1 year ago • 3 comments

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

karun-verghese avatar Feb 26 '24 14:02 karun-verghese

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?

cijothomas avatar Feb 28 '24 16:02 cijothomas

@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.

karun-verghese avatar Mar 01 '24 07:03 karun-verghese

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?

cijothomas avatar Mar 04 '24 16:03 cijothomas