I have a console application built with Visual Studio 2022. When I instrument the code with ILogger traces, and run on an Azure VM, the traces appear in the Application Insights transaction log. However, each entry is separate (i.e., they do not have Trace Ids). I added AddApplicationInsightsTelemetryWorkerService in the ConfigureServices function and used telemetry client's StartOperation method to wrap my code and traces. When I run locally, I see all the traces in AI, with traceIds as expected. When I deploy to the VM and run the same code, the traces are not collected and don't appear in AI at all. The environment variables are contained in an appsettings.json that is used both locally and Azure.

Here is the pertinent code and appsettings:

IHost host = CreateHostBuilder(args);

string version = "#{Build.BuildNumber}#";
Activity activity = new Activity($"Launch v {version} at {DateTime.Now}");

// Loop thru apps. If the time < now, run the command
ILogger<ScraperCommandModel>? logger = host.Services.GetService<ILogger<ScraperCommandModel>>();
IConfiguration? configuration = host.Services.GetService<IConfiguration>();
TelemetryClient telemetryClient = host.Services.GetRequiredService<TelemetryClient>();
//string AIConn = configuration?["APPLICATIONINSIGHTS_CONNECTION_STRING"] ?? string.Empty;

DateTime now = DateTime.Now;

if (logger == null)
{
    Console.Error.WriteLine($"Could not create logger");
    return;
}

using (telemetryClient.StartOperation<RequestTelemetry>(activity))
{
    logger.LogInformation("Version {ver} Starting. ({time})", version, DateTime.Now);
    logger.LogInformation("Completed.");
}

// implementatinon of 'CreateHostBuilder' static method and create host object
IHost CreateHostBuilder(string[] strings)
{

    IHostBuilder builder = Host.CreateDefaultBuilder();
    builder
        .ConfigureLogging((context, logging) =>
        {
            string AIKey = context.Configuration["APPLICATIONINSIGHTS_INSTRUMENTATIONKEY"] ?? string.Empty;
            string AIConn = context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] ?? string.Empty;
            logging.ClearProviders();
            logging.AddConsole();
        })
        .ConfigureServices((context, services) =>
        {
            string AIConn = context.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"] ?? string.Empty;
            services.AddLogging();
            services.AddApplicationInsightsTelemetryWorkerService((options) => options.ConnectionString = AIConn);
        });

    return builder.Build();
}

appsettings

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
        "Microsoft.AspNetCore": "Information"
        },
        "ApplicationInsights": {
            "LogLevel": {
                "Default": "Information"
            },
        "INSTRUMENTATIONKEY": "XXX",
        "ConnectionString": "InstrumentationKey=XXX;IngestionEndpoint=https://eastus2-3.in.applicationinsights.azure.com/;LiveEndpoint=https://eastus2.livediagnostics.monitor.azure.com/"
        }
    }
}
0

There are 0 best solutions below