.net-coreazure-application-insights

Core app logging requests but not traces to app insights


I noticed that my .net core app is logging requests, but not traces to my azure app insights.

I'm setting up app insights in my Startup.cs

services.AddApplicationInsightsTelemetry(options =>
            {
                options.InstrumentationKey = Configuration["ApplicationInsightsInstrumentationKey"];
            });

I'm injecting ILogger in my controller like such

private readonly ILogger<HealthCheckController> _logger;

public HealthCheckController(ILogger<HealthCheckController> logger)
{
    _logger = logger;
}

[HttpGet]
public IActionResult HealthCheck()
{
    // Custom EventId for logging
    var eventId = EventDefinition.TestMicroservice.HealthCheckController.HealthCheck;

    _logger.LogInformation(eventId, "Test Microservice health check successful.");

    return Ok("Test Microservice is running!");
}

Am I missing something? I've hit my controller multiple times and I'm seeing requests getting logged, but my custom log messages are nowhere to be found under traces.


Solution

  • So what I found was that I was able to inject TelemetryClient despite wanting to use ILogger. While I kept the above code in, I had to add the .ConfigureLogging

            protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
            {
                return new ServiceInstanceListener[]
                {
                    new ServiceInstanceListener(serviceContext =>
                        new KestrelCommunicationListener(serviceContext, "ServiceEndpoint", (url, listener) =>
                        {
                            ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");
    
                            return new WebHostBuilder()
                                        .UseKestrel()
                                        .ConfigureServices(
                                            services => services
                                                .AddSingleton<StatelessServiceContext>(serviceContext))
                                        .UseContentRoot(Directory.GetCurrentDirectory())
                                        .ConfigureAppConfiguration((context, config) =>
                                        {
                                            var configurationPackage = FabricRuntime.GetActivationContext()?.GetConfigurationPackageObject("Config");
                                            
                                            // Add Key Vault
                                            var kv = configurationPackage.Settings.Sections[KeyVaultOptions.Position].Parameters;                                        
                                            var credential = new ClientSecretCredential(kv["TenantId"].Value, kv["ClientId"].Value, kv["ClientSecret"].Value);
                                            var client = new SecretClient(new Uri(kv["Url"].Value), credential);
    
                                            config.AddAzureKeyVault(client, new AzureKeyVaultConfigurationOptions());
                                            config.Build();
                                        })
                                        .ConfigureLogging((context, logging) =>
                                        {
                                            logging.AddApplicationInsights(
                                                context.Configuration["ApplicationInsightsInstrumentationKey"]);
                                            logging.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
                                        })
                                        .UseStartup<Startup>()
                                        .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.None)
                                        .UseUrls(url)
                                        .Build();
                        }))
                };