zameb
zameb

Reputation: 850

Application Insights not Logging any trace

I see there are many similar questions, but I can't find a clear answer or documentation which works. Moreover, with our recent adoption of Net6, I'm not sure if old answers could apply.

The thing is simple: App Insights resource is not showing any data sent to ILogger.

Here the setup:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddTransient<ISomeService, SomeService>();
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddLogging();

builder.Services.AddControllers();

var app = builder.Build();

app.MapControllers();
app.Run();

As you can see, I'm not specifiying which Azure Connection string is used, because as the docs say, it should be done automatically. I fact, AppInsights logs crashes and requests, so the connection string appears to be fine.

Here I show the config at App Service Portal

enter image description here

The controller has the following:

[ApiController]
[Route("[controller]")]
public class SomeProcessController : ControllerBase
{
    private readonly ISomeService someService;
    private readonly ILogger<SomeProcessController> logger;

    public SomeProcessController(
        ISomeService someService,
        ILogger<SomeProcessController> logger)
    {
        this.someService= someService;
        this.logger = logger;
    }

    public async Task<ActionResult<bool>> SomeProcess()
    {
        try
        {
            logger.LogInformation("Start");
            var result = await someService.DoSomethingAsync(CancellationToken.None);
            logger.LogInformation("Finish");
            return result;
        }
        catch (Exception ex)
        {
            logger.LogCritical(ex, "Error while processing");
            throw;
        }
    }
}

And this is an example of what AppInsights linked resource shows:

enter image description here

I expect to see entries of "Start" and "Finish" logs, but it is not happening. When I introduce some exception as:

        try
        {
            logger.LogInformation("Start");
            throw new Exception("TEST - CRASH!");
            var result = await someService.DoSomethingAsync(CancellationToken.None);
            logger.LogInformation("Finish");
            return result;
        }
        catch (Exception ex)
        {
            logger.LogCritical(ex, "Error while processing");
            throw;
        }

I can find the "TEST - CRASH!" entry in App Insights. It should be trapped by the "catch" section, so I would also expect to find an "Error while processing" entry, but this is not the case, I only find "TEST - CRASH!". So, again, App Insights capturing everything BUT logs (I can see requests, exceptions, dependencies) but I'm sure something is missing to capture ALSO logs.

The log configuration in appSettings.json is as follows:

{
  "APPLICATIONINSIGHTS_CONNECTION_STRING": "****",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "ConnectionStrings": {
    "default": "****"
  }
}

Secondary question: how do override logger settings through the Azure App Service portal settings? Tertiary question: seems like people having problems with appInsights is not finding solutions?

I think I have not missed anything required in this official doc: https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger

That page probably is not updated to the current Net6 framework tho.

Upvotes: 0

Views: 2312

Answers (1)

Related Questions