question

NathanielWebb-4691 avatar image
0 Votes"
NathanielWebb-4691 asked ChaoDeng-MSFT edited

Logging from .NET 5 Application Doesn't Reach Application Insights

I'm setting up a new web app with Application Insights. I've installed AI, and am seeing all the expected telemetry (server requests, failed requests, etc.) in the portal--except for logging sent through ILogger.

I'm using .NET 5 and version 2.17.0 (latest stable) of the Microsoft.ApplicationInsights.AspNetCore nuget package. The connection string and instrumentation key appear to populate correctly and match my AI instance. I set the default LogLevel for ApplicationInsights to "Trace" in appsettings.json, and I'm attempting a log of every level just to make sure. My code is functionally identical to the sample code under ASP.NET Core applications here: https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger

Most importantly, the Output window in VS shows that when I call the logger, trace telemetry is being sent correctly. Yet I don't see any logging in Application Insights! I'm absolutely stumped. Any assistance in unraveling this problem would be much appreciated. Thank you!

dotnet-aspnet-core-general
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @NathanielWebb-4691,
Have you tried other log levels?
You can get different log levels according to your environment. This article may be helpful to you:https://stackoverflow.com/questions/51657398/ilogger-not-respecting-log-level-for-application-insights


0 Votes 0 ·

Hi @ChaoDeng-MSFT, thanks for the reply. I'm attempting every log level from 0 (trace) to 5 (critical), so I would expect something to get through regardless of my log level settings.

However, I haven't seen loggerFactory.AddApplicationInsights() before, so I will experiment with that and report back.

0 Votes 0 ·

Hi @ChaoDeng-MSFT, I tried adding loggerFactory.AddApplicationInsights() to my Startup config. It's flagged as deprecated, but since the proper configuration wasn't working I thought I'd give it a try. It works--sort of.

I've gotten logs to appear in the traces table in the AI logs a couple of times, but it's not consistent. For example, I ran my program, fired off the logging, and saw it appear in AI a few minutes later. I left the program running, and a few minutes later, fired off the logging again. Those logs never appeared in AI. Not only had I not changed anything, I didn't even stop and start the application. (I also tried that, with no luck.)

So I'm stumped again! This was working, but now it seems not to be. Any ideas?

Thanks!

0 Votes 0 ·

Hi @NathanielWebb-4691 ,
You can use following code to set log level in program.cs:


 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
     WebHost.CreateDefaultBuilder(args)
     .UseStartup<Startup>()
     .ConfigureLogging(
         builder =>
         {
    
             builder.AddApplicationInsights("app insights instrumentation key");
    
             // You can set log level here
             builder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>
                              ("", LogLevel.Information);
         }
     );

You can try the examples given in the above official Microsoft documents:https://docs.microsoft.com/en-us/azure/azure-monitor/app/ilogger#aspnet-core-applications




0 Votes 0 ·

0 Answers