Lotzi11
Lotzi11

Reputation: 549

Application Insights' Logs Not Writing to Traces Log

I am adding Application Insights (AI) to my web API service by following this page Application Insights Instructions. I managed to get my service to connect to AI and I am able to see when my service preforms a post, get, etc. I also placed log calls through my service, but none of them are being written to my AI's Traces log.

Traces Log

I made sure to setup my Startup.cs and appsettings.json files to contain the new code needed to run AI in throughout my service, and the logging data need to have AI grab logs debug and up.

Startup.cs

public void ConfigureServices(IServiceCollection services)
{         
    services.AddApplicationInsightsTelemetry();
}

appsettings.json

appsettings.json

Logging Example

public async Task ProcessQueueAsync(dBData dbContext)
{
    // _logger is of type ILogger<[INSERT CLASS NAME]>
    _logger.LogDebug("This is a test log by Lotzi11.");
    await ProcessQueueAsyncSingle(dbContext, CancellationToken.None);
}

Can someone help me figure out why my logs are not being sent to AI?

Upvotes: 5

Views: 6793

Answers (2)

Lotzi11
Lotzi11

Reputation: 549

While I was working on solving my problem, I found out that my company uses Serilog to handle logging, so I had to alter my project so Serilog would also send logs to AI. I modified my code using the following page serilog-sinks-applicationinsights.

This led my to realize that even though I followed Microsofts instructions on setting up AI, my ILogger class is not properly setup to handle sending logs to AI. To fix that, I alter my Startup.cs's constructor:

    public Startup(IHostEnvironment environment, IConfiguration configuration)
    {
        var env = new Environment(environment.EnvironmentName);

        _systemConfiguration = new SystemConfiguration(env, configuration);
        _systemConfiguration.Validate();

        Log.Logger = new LoggerConfiguration().Enrich.FromLogContext().WriteTo.ApplicationInsights(_systemConfiguration.BaseConfiguration["APPINSIGHTS_INSTRUMENTATIONKEY"], TelemetryConverter.Traces).CreateLogger();
        using var provider = new SerilogLoggerProvider(Log.Logger);            
        _logger = provider.CreateLogger(nameof(Startup));
    }

After adding AI to Log.Logger, my logs began showing up in my AI's page.

Upvotes: 3

Ivan Glasenberg
Ivan Glasenberg

Reputation: 29940

Your code configuration and appsettings.json are correct. I can see these logs in AI as per your settings at my side.

One thing you should know is that, it may take a few minutes for these data to arrive in AI server. Please wait for a few minutes, like 5 minutes or more, then query these data again from azure portal -> application insights.

And here is a simple way to check if the data is sent to AI. In visual studio, when running the project, you can search the logs in visual studio output window. If you can find the logs there, then it should be sent to AI:

Search in visual studio output window:

enter image description here

If you still cannot see the these logs in AI, you should also check if you have set something like filter or sampling in your code.

Upvotes: 6

Related Questions