Performance log not capturing in Application Insights

123 Views Asked by At

I am working on .net core 3.1 web api where I want to capture performance for every request made to api. Following is the code I have done that does not log anything in Application Insights Performance but when I explicitly registered the middleware using

    var telemetryClient = app.ApplicationServices.GetRequiredService<TelemetryClient>();
    app.UseApplicationInsightsMiddleware<ApplicationInsightsMiddleware>(telemetryClient);

performance logs are getting captured.

As per couple of articles in internet, by just doing below settings performance log can be captured but its not working. Please suggest what I am doing wrong?

appsettings.Development.json

    {
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Function": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Information"
      }
    }
  },  
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=xxxxx-xxx-xxx-xxxx-xxxxxx;IngestionEndpoint=https://eastus-8.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus.livediagnostics.monitor.azure.com/",
  },
  "MaxFileSize": 25,
  "AllowedHosts": "*",
  "LoggingConfiguration": {
    "Log4Net": "log4net.config",
    "ApplicationInsights": "xxxxx-xxx-xxx-xxxx-xxxxxx",
    "ApplicationInsightsLiveMetricsApiKey": "xxxxxxxxxxxxxxxxxxxxxxx"
  }
}

Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        Configuration config = Configuration.GetSection("LoggingConfiguration").Get<Configuration>();

        services.AddTransfastLogger((log4Net_Options) =>
        {
            log4Net_Options.ConfriguationFileName = config.Log4Net;
        }, (applicationInsightsOptions) =>
        {
            applicationInsightsOptions.InstrumentationKey = config.ApplicationInsights;
            applicationInsightsOptions.ConnectionString = string.Empty;
            applicationInsightsOptions.LiveMetricsAuthenticationApiKey = config.ApplicationInsightsLiveMetricsApiKey;
        });
        
        services.AddControllers();
        services.AddApplicationInsightsTelemetry();
    }

WeatherForecastController.cs

[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();
    }
}
1

There are 1 best solutions below

5
Harshitha On BEST ANSWER

I am able to log Traces and see the same in Performance section.

I am using VS 2022 17.8.0 version.

Configured Application Insights from the Connected Services.

enter image description here

  • As mentioned, I haven't added any middleware related to the Telemetry.
  • My Startup.cs file has only the below line of code to log Application Insights Telemetry.
services.AddApplicationInsightsTelemetry(Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

OR

services.AddApplicationInsightsTelemetry();
  • I have configured the same settings as yours in appsettings.Development.json file.

  • I have tried with both latest 2.21.0 and 2.15.0 older versions of Microsoft.ApplicationInsights.AspNetCore package.

Make sure you are executing the Controller Action Method

https://localhost:portno./weatherforecast

Output: enter image description here

  • Check under the Server, Operations tab.

enter image description here