My Program
class looks like this
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((webHostingBuilder, configBuilder) =>
{
configBuilder.SetBasePath(webHostingBuilder.HostingEnvironment.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{webHostingBuilder.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
})
.ConfigureLogging((webHostingBuilder, logging) =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddDebug();
logging.AddApplicationInsights(
webHostingBuilder.Configuration.GetValue<string>("Logging:ApplicationInsights:InstrumentationKey"));
})
.UseStartup<Startup>();
}
and on my Startup
on ConfigureServices
services.AddApplicationInsightsTelemetry(configuration["Logging:ApplicationInsights:InstrumentationKey"]);
And this is my controller class:
public class AccountController : BaseController
{
private ILogger _logger;
public AccountController(ILogger<AccountController> logger)
{ ... }
[HttpGet]
public async Task<IActionResult> Login(string returnUrl)
{
_logger.LogError("SuperError");
}
}
Is there anything else that I need to configure in order to save the logs? I couldn't see anything here while doing my example
Your code looks good. Sometimes it can take a while for logs to show up in
App Insights
. Example configuration that works given a correctInstrumentationKey
:https://learn.microsoft.com/en-us/azure/azure-monitor/app/ilogger#im-using-the-standalone-package-microsoftextensionsloggingapplicationinsights-and-enabling-application-insights-provider-by-calling-builderaddapplicationinsightsikey-is-there-an-option-to-get-an-instrumentation-key-from-configuration
Startup.cs
appsettings.json
WeatherForecastController.cs