Zinov
Zinov

Reputation: 4119

AppInsights is only saving telemetry data but not saving logs with a .Net Core web app hosted on an IIS

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

enter image description here

Upvotes: 1

Views: 299

Answers (1)

Ogglas
Ogglas

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:

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

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

Related Questions