Here is my .net 6 program.cs basic code. In the Following sample code I am trying to write basic logs into Application insights. I have Nuget package reference in my project file
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.ApplicationInsights.DependencyCollector" Version="2.21.0" />
I am using classic application insights
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging.ApplicationInsights;
using Microsoft.ApplicationInsights.AspNetCore.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services?.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions
{
ConnectionString = "InstrumentationKey=19233009-a529-494c-9b4b-3fd57d363357;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/;LiveEndpoint=https://centralus.livediagnostics.monitor.azure.com/"
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.Logger.LogInformation("TEST LOGS");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
Any suggestions why is it not logging "Test logs" into my application insights logs
Update
As suggested by @jasonc. I followed the instructions from example https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#example-programcs, and now my logs are getting written from program.cs file to my application insights. But still not logging Ilogger logs from my controller.
I have the following code in my controller, but it is not getting written to application insights.
public class MyController : Controller
{
private readonly ILogger<MyController> _logger;
public MyController(ILogger<MyController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("From myController, running Index.");
}
}