4

I have an ASP.NET Core application running as Azure App Service. Azure Application Insights is enabled (I followed these instructions). The problem is my instance of Azure Insights on Azure Portal isn't showing any useful data except for Live Metrics (see the screenshot). As you can see there are multiple requests and custom events on the screenshot.

However, when I open Transaction search it shows nothing (see the screenshot). Events page is empty as well (see the screenshot).

So far I double-checked an InstrumentKey. Also I tried to use ConnectionString instead of InstrumentKey, but it didn't help.

My app is running on .NET Core 3.1. I installed the latest version of Microsoft.ApplicationInsights.AspNetCore package which is 2.19.0.

Here is how logging is configured in Program.cs:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureLogging(builder =>
            {
                builder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Information);
            });

And below is code from Startup.cs:

services.AddApplicationInsightsTelemetry(new ApplicationInsightsServiceOptions 
        {
            ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING")
        });

LogLevel is also configured in appsettings.json:

"Logging": {
"LogLevel": {
  "Default": "Warning"
},
"ApplicationInsights": {
  "LogLevel": {
    "Default": "Information"
  }
}

Update: My Admin who has more permissions can see all data, including events, performance operations etc. So I suppose there's something to do with permissions. Though it's strange that I'm not seeing any warning messages. The Admin assigned me more roles (see the screenshot), but it didn't make any difference.

I would appreciate any help on this issue!

  • Try logging some fake warnings(using the ILogger), or reduce your log level to info, and log info level messages and check if those appear - it usually takes 3 to 5 mins before it appears in the App Insights portal page/charts. – Anand Sowmithiran Dec 06 '21 at 11:28
  • Read this particular [section](https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core#how-do-i-customize-ilogger-logs-collection) of the FAQ. – Anand Sowmithiran Dec 06 '21 at 11:31
  • @AnandSowmithiran, thanks for advice, but it didn't help. I even set my log level to Trace. The Live Metrics page is showing lots of Trace events now in the Sample telemetry section. So looks like Application Insights is receiving data, but not saving it... – Rashid Kashafutdinov Dec 06 '21 at 11:57
  • see the answers for this [SO question](https://stackoverflow.com/questions/60216701/application-insights-showing-in-live-metrics-but-not-in-log-analytics), sometimes the Instrumentation key not entered right. – Anand Sowmithiran Dec 06 '21 at 12:18
  • Are you solely using `ILogger` or (also) the `TelemetryClient`? Can you post the code/config that sets the instrumentation key and the loglevels? – Peter Bons Dec 06 '21 at 13:23
  • @AnandSowmithiran, I compared the connection strings and they are identical. So the problem must be somewhere else – Rashid Kashafutdinov Dec 07 '21 at 09:12

2 Answers2

7

After tearing almost all hair off my head I finally solved the issue! Turned out the instance of Azure Application Insights was linked to a Log Analytic Workspace that belonged to a Resource Group to which I didn't have access. So logs were stored properly, but I didn't have permission to read them. My admin solved the issue by creating a new instance of Azure Application Insights which was linked to a Log Analytic Workspace within my Resource Group. To anyone who isn't familiar with Log Analytic Workspace - it can be specified when you create a new instance of Azure Application Insights (see the screen).

Thanks everyone for trying to help me!

UPDATE: As Jonathan L. mentioned in the comments, instead of creating a new Application Insights instance, one can just change Workspace in Properties.

  • It's worth updating your answer to mention that you can change the _Log Analytics workspace_ for an _Application Insights_ resource without having to create a new _Application Insights_ resource. Go to _Properties_ for the _Application Insights_ resource and under _WORKSPACE_ there is a _Change workspace_ link. Thank you for this answer though, it pointed me in the right direction. – Jonathan L. Aug 11 '22 at 13:12
1

I have tried the same and can able to see the logs inside portal .

As Peter Bons suggested make sure that you are using ILogger in your controller .

Here are the steps i have followed .

  • I have download an sample project from GitHub and after extract open project in Visual studio and configure with Application insight telemetry . Updated latest Microsoft.ApplicationInsights.AspNetCore to 2.19.0

enter image description here

And added instrumentation key in my appsettings.json which copied from Azure portal>Application insight(my applnsight)>overview.

{
  "Logging": {
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Debug",
        "Microsoft": "Error"
      }
    },
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "InstrumentationKey": "mykey",
    "ConnectionString": "InstrumentationKey=6xxxxxx-xxxxx-xxxx-xxxx-0000000xxxxxxxx.in.applicationinsights.azure.com/"
  }
}
  • Ilogger configuration in my controller.cs
namespace ApplicationInsightsTutorial.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

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

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {

            var iteracion = 4;

            _logger.LogDebug($"Debug {iteracion}");
            _logger.LogInformation($"Information {iteracion}");
            _logger.LogWarning($"Warning {iteracion}");
            _logger.LogError($"Error {iteracion}");
            _logger.LogCritical($"Critical {iteracion}");

            try
            {
                throw new NotImplementedException();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, ex.Message);
            }

            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

In startup.cs added

 public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddApplicationInsightsTelemetry();//telemetry added
        }

After all that above configuration run the application and navigate to Azure portal to check the logs .

enter image description here

Make sure that you have provided the log information which you want to check as example in my controller.cs .

from the logs we can see the exceptions/errors with line of code as well . Here are some screenshot for reference: enter image description here

enter image description here

enter image description here

For more information please refer this SO Thread .

AjayKumarGhose
  • 4,257
  • 2
  • 4
  • 15