Psychonaut007
Psychonaut007

Reputation: 197

Why only few logging are being sent to Azure Application insight through ServerTelemetryChannel?

This is my program in which I am using ServerTelemetryChannel because I read it is better than InMemoryChannel. But in this case only couple of logging are being sent to Azure Application Insight. Inside my try block I have a lot of logging and method calling which also contains logs. I have read the documentation but it is not clear what values should I add, like in case of MaxTelemetryBufferCapacity property, what is the best value I can add here.

Secondly, this program will work as webjob. So according whats the best practice to add ServerTelemetryChannel logging. What all properties from ServerTelemetryChannel I should use?

public class Program
    {
        private static IServiceCollection services;

        private static IServiceProvider serviceProvider;
        private static ILogger<Program> logger;
        private static TelemetryClient telemetryClient;
  static void Main()
        {
            services = new ServiceCollection()
               .AddLogging(loggingBuilder => loggingBuilder
                   .AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("",
                       LogLevel.Trace))
               .AddSingleton(typeof(ITelemetryChannel), new ServerTelemetryChannel())
               .AddApplicationInsightsTelemetryWorkerService(
                   ConfigurationManager.AppSettings["ApplicationInsightKey"]);

            serviceProvider = services.BuildServiceProvider();
            logger = serviceProvider.GetRequiredService<ILogger<Program>>();
            telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

            using (telemetryClient.StartOperation<RequestTelemetry>("Program Name"))
            {
                logger.logInformation("Execution started")
                try
                {
                    ..Execution...
                }
                catch (Exception e)
                {
                    logger.logError(e.ToString())
                }
                finally
                {
                   logger.logInformation("Execution completed")
                   telemetryClient.Flush();
                }
            }
           
        }


Upvotes: 0

Views: 109

Answers (1)

Peter Bons
Peter Bons

Reputation: 29770

You should not have to modify the ServerTelemetryChannel properties, the defaults are just fine. Also, ServerTelemetryChannel is the default so you do not need to add it manually.

Apart from the log level, there are some other factors that can cause telemetry to not appear. One is sampling and the other one is not flushing the channel internal buffer so any telemetry still in the buffer is not send to the Azure backend upon closing the application. Do keep in mind you need to wait for a short period of time after calling Flush() as this can take some time and is an async operation.

In the code below sampling is disabled and the buffer is flushed correctly:

static void Main()
{
    services = new ServiceCollection()
        .AddLogging(loggingBuilder => loggingBuilder
            .AddFilter<Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider>("",
                LogLevel.Trace))
        .AddApplicationInsightsTelemetryWorkerService((options) =>
        {
            options.InstrumentationKey = "xxxx-xxx-xxx-xxx-xxxx";
            options.EnableAdaptiveSampling = false;
        });

    serviceProvider = services.BuildServiceProvider();
    logger = serviceProvider.GetRequiredService<ILogger<Program>>();
    telemetryClient = serviceProvider.GetRequiredService<TelemetryClient>();

    using (telemetryClient.StartOperation<RequestTelemetry>("Program Name"))
    {
        logger.LogTrace("Execution started");
        try
        {
                    
        }
        catch (Exception e)
        {
            logger.LogError(e.ToString());
        }
        finally
        {
            logger.LogInformation("Execution completed");
        }
    }

    telemetryClient.Flush();
    Task.Delay(5000).Wait();
}

Upvotes: 1

Related Questions