0

.NET6, Microsoft.ApplicationInsights.AspNetCore version 2.21.0

My AppInsights works, but I also have this trace warning in Azure:

AI: TelemetryChannel found a telemetry item without an InstrumentationKey. This is a required field and must be set in either your config file or at application startup.

How to fix it?

I'm using recommended ConnectionString (instead of InstrumentationKey).

My appsettings.json:

"ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=xxxx;IngestionEndpoint=https://yyy.azure.com/;LiveEndpoint=https://zzz.azure.com/",
    "LogLevel": {
        "Default": "Information"
    }
}

My startup.cs:

// AppInsights options
var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
{
    EnableDependencyTrackingTelemetryModule = false,
};

services.AddApplicationInsightsTelemetry(aiOptions);
Saibamen
  • 610
  • 3
  • 17
  • 43

1 Answers1

0

I have tried to Integrate Application Insights in .NET Core6 Web App using the Application Insights Connection String and able to see the traces without any warnings.

  • You are writing the code inStartup.cs, .NET 6 doesn't require Startup.cs file, the code can be configured within the Program.cs file.

  • In .NET 6 both Program.cs and Startup.cs configurations are unified into a single file Program.cs.

My appsettings.json

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=****;IngestionEndpoint=https://*****.in.applicationinsights.azure.com/;LiveEndpoint=https://****.livediagnostics.monitor.azure.com/"
  }
}

Program.cs

var builder = WebApplication.CreateBuilder(args);

var aiOptions = new Microsoft.ApplicationInsights.AspNetCore.Extensions.ApplicationInsightsServiceOptions
{
    EnableDependencyTrackingTelemetryModule = false,
};
builder.Services.AddApplicationInsightsTelemetry(aiOptions);
builder.Services.AddControllersWithViews();
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();

Traces for MVC

 public IActionResult Index()
        {
            _logger.LogInformation("Log Information from Controller.");
            _logger.LogDebug("Debug Message from the Controller.");
            _logger.LogWarning("Log Warning.");
            _logger.LogError("Log Error.");
            return View();
        }

Traces for Web App

var app = builder.Build();
ILogger logger = app.Logger;
logger.LogInformation("Test logs");
logger.LogWarning("Iam a Warning Message");

Traces for MVC App enter image description here

Traces for Web App enter image description here

Harshitha
  • 3,784
  • 2
  • 4
  • 9
  • This is from your localhost or AppService from Azure? I have this warning only on AppServices (Linux Docker) every 15 minutes, in localhost on Windows I don't see these. – Saibamen Nov 22 '22 at 09:29
  • 1
    This is from local host. Will try with the `Azure Linux App service` and let you know. – Harshitha Nov 22 '22 at 09:30