using Microsoft.ApplicationInsights;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp8
{
class Program
{
static IServiceCollection services = new ServiceCollection()
.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Trace))
.AddApplicationInsightsTelemetryWorkerService("Application_Key");
static IServiceProvider serviceProvider = services.BuildServiceProvider();
static ILogger<Program> logger = serviceProvider.GetRequiredService<ILogger<Program>>();
static TelemetryClient telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();
static void Main(string[] args)
{
using (telemetryClient.StartOperation<RequestTelemetry>("AppointmentPatientCommunication"))
{
logger.LogInformation("1st");
hero();
logger.LogError("2nd");
telemetryClient.TrackTrace("Here is the error");
telemetryClient.Flush();
}
}
static void hero()
{
using (telemetryClient.StartOperation<RequestTelemetry>("AppointmentPatientCommunication"))
{
logger.LogInformation("2nd");
telemetryClient.Flush();
}
}
}
}
I uploading this console application as my webjob to make a log in application insight. I am trying to avoid the use of task.delay()
so that I can get real-time logging at perfect timing. I am uploading this webjob triggered manually, but I see no entry in my application insights. Could anyone help me out with this one?
Telemetry is not sent instantly. Telemetry items are batched and sent by the ApplicationInsights SDK. In Console apps, which exits right after calling Track() methods, telemetry may not be sent unless Flush() and Sleep/Delay is done before the app exits as shown in full example later in this article. Sleep is not required if you are using InMemoryChannel. There is an active issue regarding the need for Sleep which is tracked here: link.
So there are two types of channels: InMemoryChannel and ServerTelemetryChannel For more details about the both the channels click on this link.
In my program to deal with the issue, I used InMemoryChannel. In the below code, I have shown a portion of code to show how I added it in my program.
static IServiceCollection services = new ServiceCollection()
.AddLogging(loggingBuilder => loggingBuilder.AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("", LogLevel.Trace))
.AddSingleton(typeof(ITelemetryChannel), new InMemoryChannel())
.AddApplicationInsightsTelemetryWorkerService("Application_Key");
The Nuget Package I am using is Microsoft.ApplicationInsights.Channel