1

We're trying to get Application Insights enabled for our ASP Net Core 7.0 C# application. We've used a few sources, including this Microsoft site:

https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger?WT.mc_id=DOP-MVP-5001942&tabs=dotnet6

The Azure Application Insights module itself seem correctly configured, for example it's showing app requests correctly.

However our custom ILogger messages in our C# application don't show up in the Azure App Insights traces section.

We've based our setup below from various other examples on how to configure ILogging, so we're not sure which aspect of our setup is wrong, but suspect it's in our code somewhere.

C# Program

    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        ...
        
        builder.Services.AddApplicationInsightsTelemetry();

        builder.Logging.AddApplicationInsights(
            configureTelemetryConfiguration: config =>
                 config.ConnectionString = configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"],
            configureApplicationInsightsLoggerOptions: options => { }
        );
        
        ... 

        var app = builder.Build();
    }        

appsettings.json

Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "ApplicationInsights": "Debug"
    }
  }

csproj file

  <ItemGroup>
    ...
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
    ...
  </ItemGroup>

Azure Web App Settings Configuration

enter image description here

Artie Leech
  • 347
  • 2
  • 14

2 Answers2

1

Is there anything in the config below that looks incorrect?

Yes, the config is wrong, take a look at the right way by examining the following configuration:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug"
      }
    }
  }
}

As you can see the ApplicationInsights loglevel is set in a subsection.

Peter Bons
  • 26,826
  • 4
  • 50
  • 74
  • Once we applied the changes as described here by Peter, our C# application ILogger logs started to show in Application Insights. Note that the ApplicationInsights config element is directly below the root Logging element, NOT within the LogLevel element. – Artie Leech Jun 05 '23 at 13:41
0

Thanks @Peter Bons for the comment.

Yes, as mentioned by Peter Bons, your configuration file is not having all the required settings.

If you are running ASP Net Core C# application locally, then we need to set the Application Insights Connection String/Instrumentation Key in the appsettings.json file.

My .csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
    <ItemGroup>     
        <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />        
    </ItemGroup>
</Project>

My appsettings.json file:


{
  "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/"
  }
}

My Program.cs file:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();
builder.Services.AddApplicationInsightsTelemetry();

//var abc = builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"];

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();

My Controller.cs:

 private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            _logger.LogWarning("Test Warning trace - June 5");
            _logger.LogError("Test Error - June 5");

            return View();
        }

Application Insights Transaction Search:

enter image description here

You can see I am able to log Exceptions as well.

Anand Sowmithiran
  • 2,591
  • 2
  • 10
  • 22
Harshitha
  • 3,784
  • 2
  • 4
  • 9