skip to Main Content

I am totally in despair and probably is it just a minor detail I am missing, but I cannot figure it out.

Description

I am following Application Insights for Worker Service applications (non-HTTP applications), and here is some details.
My package references:

<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" /> 
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.0" />

My secret.json

"APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=xxxx;IngestionEndpoint=https://somewhere-0.in.applicationinsights.azure.com/",
  }

The code follows the link mentioned, but just to put it here. Program.cs

services.AddLogging();
services.AddHostedService<TimedHostedService>();
services.AddApplicationInsightsTelemetryWorkerService();
 

Worker.cs

_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
using (_telemetryClient.StartOperation<RequestTelemetry>("operation"))
{
   _logger.LogWarning("A sample warning message.");
   _logger.LogInformation("Calling bing.com");
   var res = await _httpClient.GetAsync("https://bing.com");
   _logger.LogInformation("Calling bing, status:" + res.StatusCode);
   _telemetryClient.TrackEvent("Bing call event completed");
   _telemetryClient.Flush();
}
await Task.Delay(5000, stoppingToken);

In Visual Studio I can see events ticking in.

Visual Studio Application Insights

Problem

After days, I have not been able to get anything to be apper in Azure Portal. I wanted one web application and some containers to report to the same log analytics and azure application insights. However, no luck so far.
The web application whom is deployed to Azure app service reports fine.
The containers are supposed to run on a local datacenter (on premise), but now I am running them in a Docker Desktop.

Questions.

I simply have no idea how to figure out what I am missing, so it will just be a series of questions what pops into my head. Is there something blocking from Docker? Must I adjust something in Azure Portal? Do I need to set something up on my computer? Is there a settings somewhere stating that the app should not transmit to application insights because it is a development enviroment? (I could go on for ever).
I am pretty sure this has an easy answer, but Google is not my friend these days.
Any help will be appreciated.

2

Answers


  1. I followed the same MSDOC you shared. I can be able to get all telemetry data in Application Insights. I hope you followed all the things mentioned in the document.

    Please check below to overcome the issue

    • Check your Package References what are all packages you are using make sure to add them to your solution.
    • Make sure the App Insights Connection settings values should be valid.
    • Add the WorkerService in your Program.cs like
    services.AddLogging(<Your Logger Provider>);
    # make sure to access of your Hosted Service
    services.AddHostedService<TimedHostedService>();
    services.AddApplicationInsightsTelemetryWorkerService();
    

    Note that ApplicationInsightsServiceOptions in this SDK is in the namespace Microsoft.ApplicationInsights.WorkerService as opposed to Microsoft.ApplicationInsights.AspNetCore.Extensions in the ASP.NET Core SDK.

    • processing the Custom Operation Tracking in Application Insights
    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
    using (_telemetryClient.StartOperation<RequestTelemetry>("operation"))
    {
       #_logger will be appear on Console 
       
       _logger.LogWarning("A sample warning message.");
       _logger.LogInformation("Calling bing.com");
       var res = await _httpClient.GetAsync("https://bing.com");
       _logger.LogInformation("Calling bing, status:" + res.StatusCode);
      
       # _telemetryClient will be appear in Application Insights
       
       _telemetryClient.TrackEvent("Bing call event completed");
       _telemetryClient.Flush();
    }
    await Task.Delay(5000, stoppingToken);
    

    Refer usage of Custom Operation Tracking in Application Insights

    Login or Signup to reply.
  2. services.AddApplicationInsightsTelemetryWorkerService();
    With this method, the connection string must be part of application’s IConfiguration. I am not sure if secrets.json is automatically loaded into IConfiguration.

    You can confirm this by using the following snippet:

     var aiOptions = new ApplicationInsightsServiceOptions();
     aiOptions.ConnectionString = "put it here";
     services.AddApplicationInsightsTelemetryWorkerService(aiOptions);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search