I'm trying to log some messages like error messages into app-insights using ILogger
.
I have already implemented ILogger
and it works fine for logging into Console, but it does not log anything into app-insights.
The Docs seems to be not up to date, since there is no method called AddApplicationInsights
in ILoggingBuilder
interface.
Therefore I've used the method AddAzureWebAppDiagnostics
instead.
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
// Add Azure Logging
logging.AddAzureWebAppDiagnostics();
logging.AddConsole();
})
.UseApplicationInsights()
.UseStartup<Startup>()
.Build();
public class Startup
{
private readonly ILoggerFactory _loggerFactory;
public Startup(IConfiguration config, ILoggerFactory loggerFactory)
{
Configuration = config;
_loggerFactory = loggerFactory;
}
public void ConfigureServices(IServiceCollection services)
{
ILogger logger = _loggerFactory.CreateLogger<BasicBot>();
logger.LogInformation("THAT IS A TEST MESSAGE");
}
}
It logs fine into the Console BUT I see nothing in app-insights!
Hint: I'm using dotnet core 2.1 and debug in Development from visual studio.
Hint: app-insights works fine and logs the requests using ITelemetry
. But I need ILogger
too, to log different messages from many locations in the code!
The method AddApplicationInsights
is included in this package Microsoft.Extensions.Logging.ApplicationInsights.
I setup a .net core 2.1 web app, and configure application insight as well as installing this package Microsoft.Extensions.Logging.ApplicationInsights , version 2.9.1. The logs can be shown both in console and portal.
In code:
Test result - logs in azure portal: