Reputation: 4119
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
Upvotes: 1
Views: 299
Reputation: 69968
Your code looks good. Sometimes it can take a while for logs to show up in App Insights
. Example configuration that works given a correct InstrumentationKey
:
Startup.cs
services.AddApplicationInsightsTelemetry();
appsettings.json
"ApplicationInsights": {
"InstrumentationKey": "putinstrumentationkeyhere"
}
WeatherForecastController.cs
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public ActionResult<IEnumerable<WeatherForecast>> Get()
{
_logger.LogInformation("WeatherForecastController Get");
Upvotes: 1