0

Please note that this post is not a duplicate of this one or at least the discussion in that post did not work. I have a .NET 6.0 console app with the following basic Application Insights configuration:


using var channel = new InMemoryChannel();

try
{
    IServiceCollection services = new ServiceCollection();
    services.Configure<TelemetryConfiguration>(config => config.TelemetryChannel = channel);
    services.AddLogging(builder =>
    {
        builder.AddFilter<ApplicationInsightsLoggerProvider>("Microsoft", LogLevel.Error);
        builder.AddApplicationInsights("000000-0000-0000-0000-1111111");
        builder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
    });
    
    IServiceProvider serviceProvider = services.BuildServiceProvider();
    ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();
    var now = DateTime.UtcNow;
    logger.LogInformation("Logger is working... {Now}", now);
    logger.LogError("Logger is issuing an error {Now}", now);
    logger.LogWarning("Logger is issuing a warning {Now}", now);
    logger.LogError(new Exception("Some ficititious exception"), "This is an exception messsage {Now}", now);
}
finally
{
    // Explicitly call Flush() followed by Delay, as required in console apps.
    // This ensures that even if the application terminates, telemetry is sent to the back end.
    channel.Flush();

    await Task.Delay(TimeSpan.FromMilliseconds(1000));
}

I am able to see the logs in the Application Insights but the errors are logged as "Information" level in there despite calling logger.LogError("Logger is issuing an error {Now}", now);. The other error log message which has an Exception object looks fine but my challenge is that the logger.LogError("Logger is issuing an error {Now}", now); is not working! Any idea or guidance on getting this problem resolved?

Arash
  • 3,628
  • 5
  • 46
  • 70
  • can you share a screenshot of `Logger is issuing an error`? – Shahar Shokrani Jun 16 '22 at 18:28
  • @ShaharShokrani I've got it working in this small POC project. However I have a project using Serilog that refrains from putting the right log level in application insights. – Arash Jun 16 '22 at 18:35
  • @ShaharShokrani would you mind taking a look at this post? https://stackoverflow.com/questions/72650961/serilogs-application-insight-sink-does-not-put-the-right-severity-level-on-azur – Arash Jun 16 '22 at 19:26

0 Answers0