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?