azureazure-application-insightstelemetry

How to configure TelemetryConfiguration without dependency injection?


I use Application Insights in a webjob. Current I don't use dependency injection. I need to configure the TelemetryChannel to be able to Flush the provider before exiting my webjob. Is it possible to configure/set/inject the TelemetryConfiguration into the logging provider for App Insights without DI?

I use the following statement to add the logging provider.

    builder.AddApplicationInsightsWebJobs(o => o.ConnectionString = connStr).SetMinimumLevel(LogLevel.Information);

Solution

  • configure TelemetryConfiguration without dependency injection?

    To Configure Telemetry without DI, we need to pass Instrumentation Key or ConnectionString to the TelemetryConfiguration.

    I have tried to set the Keys from appsettings.json

    Install the NuGet Package Microsoft.ApplicationInsights.Web.

    enter image description here

    My appsettings.json file

    {
      "Logging": {
        "ApplicationInsights": {
          "LogLevel": {
            "Default": "Debug",
            "Microsoft": "Error"
          }
        },
        "LogLevel": {
          "Default": "Information",
          "Microsoft.AspNetCore": "Warning"
        }
      },
    
      "AllowedHosts": "*",
      "ApplicationInsights": {
        "ConnectionString": "****",
        "Instrumentationkey": "****"
      }
    }
    

    Thanks @rogerpence.dev for the code snippet.

    I have tried to follow the given code but got the below error.

     var config = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()
    .Build(); 
    

    enter image description here

    For now, I have set the keys in Program.cs file itself.

    Is it possible to configure/set/inject the TelemetryConfiguration into the logging provider for App Insights without DI?

    In Visual Studio, right click on the solution root folder => Add => Connected Service.

    enter image description here

    enter image description here

    My .csproj file

    <Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Publish">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.ApplicationInsights" Version="2.21.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.Web" Version="2.21.0" />
        <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.15.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
        <PackageReference Include="Microsoft.Extensions.Primitives" Version="7.0.0" />
      </ItemGroup>
      <ItemGroup>
        <None Update="appsettings.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
        <None Update="Settings.job">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    </Project>
    

    We have MSDoc which explains Configurating Telemetry without Dependency Injection.

    Program.cs

    using Microsoft.ApplicationInsights;
    using Microsoft.ApplicationInsights.DependencyCollector;
    using Microsoft.ApplicationInsights.Extensibility;
    using System.Net.Http;
    using System.Threading.Tasks;
    using static System.Net.Mime.MediaTypeNames;
    
    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {            
                TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
                configuration.ConnectionString = "Copy the Connectionstring from AppInsights";
                //configuration.InstrumentationKey = "****";
                var telemetryClient = new TelemetryClient(configuration);
                telemetryClient.TrackTrace("Hello World!");
                telemetryClient.TrackTrace("This is the Telemetry Logs!");
                telemetryClient.Flush();
            }      
        }
    }
    

    OutPut:

    enter image description here