Steffen Mangold
Steffen Mangold

Reputation: 1144

ApplicationInsights, Net core and HostBuilder (not Asp.net)

Can someone explain to me how to get advanced ApplicationInsights modules like PerformanceCollector working on a simple IHostService/HostBuilder .Net core application?

With a setup like this, I managed to get ILogger logs into ApplicationInsights:

var builder = new HostBuilder()
    .ConfigureServices((hostContext, services) =>
    {
        services.AddLogging();

        // ApplicationInsights initializer
        services.Configure<TelemetryConfiguration>(config => config.InstrumentationKey = "52xxxx8a-2xx2-4xx8-bxx6-4xxxxxxxxxxxx2");
        services.Configure<SamplingTelemetryProcessor>(config => config.SamplingPercentage = 10.0);
        services.AddSingleton<ITelemetryModule, DependencyTrackingTelemetryModule>();
        services.AddSingleton<ITelemetryModule, PerformanceCollectorModule>();
        services.AddSingleton<ITelemetryModule, AppServicesHeartbeatTelemetryModule>();
        services.AddSingleton<ITelemetryProcessor, SamplingTelemetryProcessor>();
        services.AddSingleton<IApplicationIdProvider, ApplicationInsightsApplicationIdProvider>();
        services.AddSingleton<TelemetryClient>();
    })
    .ConfigureLogging(configureLogging =>
    {
        configureLogging.AddConsole();
        configureLogging.AddDebug();
        configureLogging.AddApplicationInsights();
    });

builder.Build().Run();

But for example PerformanceCollectorModule is doing nothing. No performance metrics are sended.

I try to look how Asp.Net Core Extention is working, but I don't understand it.

Packages used:

<PackageReference Include="Microsoft.ApplicationInsights" Version="2.10.0" />
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.10.0" />
<PackageReference Include="Microsoft.ApplicationInsights.PerfCounterCollector" Version="2.10.0" />
<PackageReference Include="Microsoft.ApplicationInsights.WindowsServer" Version="2.10.0" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.2.0-preview" />
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.4" />
<PackageReference Include="Microsoft.Azure.CosmosDB.BulkExecutor" Version="2.3.0-preview2" />
<PackageReference Include="Microsoft.Azure.EventHubs" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.EventHubs.Processor" Version="3.0.0" />
<PackageReference Include="Microsoft.Azure.KeyVault" Version="3.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.6" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.10.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.9.2" />

Upvotes: 0

Views: 2129

Answers (1)

cijothomas
cijothomas

Reputation: 3171

There is a new package from Application Insights to use in non Asp.Net application as your example. Remove all the ApplicationInsights packages you have, and just add the following one:

<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.8.0-beta3" />

Then call services.AddApplicationInsightsTelemetryWorkerService(); in your ConfigureServices. (remove all other application insights related code)

And modify appsettings.json with instrumentation key as below:.

    {
      "ApplicationInsights": {
        "InstrumentationKey": "putinstrumentationkeyhere"
      },
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      }
    }

(I work in the application insights team, and this new feature is being documented officially. There will be a sample app as well once this is merged: https://github.com/Microsoft/ApplicationInsights-home/pulls)

Upvotes: 2

Related Questions