Andrew Matthews
Andrew Matthews

Reputation: 3146

(Re)attaching to an App Insights Operation from another machine/process (not using HTTP)

I have a .NET 8 distributed system in AKS where work is divided among workers using a Manager/Worker pattern. With work shared out on a Redis List. I'm aiming to get unified logging via Application Insights, but I'm facing an issue. I can't currently pass important details like SpanId, TraceId, or ParentId unless it's through HTTP calls. How can I extract these details from the ILogger instance or some other part of the system, like a thread or a Task so I can pass them via messages on the Redis list?

The workers are using TPL Dataflow. When I receive a request on the worker, I want to continue the Operation started on the master. I believe the context data should transfer smoothly from there.

I can see the data I need in the logger but I'm struggling to access it. The place where I need to access this stuff is deeply buried below the place where the operation is created, so I can't get it from the App Insights SDK itself (I assume?!?).

Debugger view of the data

This screen-grab is a view of the Scope property of the ILogger instance. I can see the data in the instance via my debugger, but I cannot get at it programmatically.

The ILogger instance and AppInsights are set up using dependency injection. These are the relevant commands I used:

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddLoggingEtc(this IServiceCollection services)
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.AddLogging(logging =>
        {
            logging.ClearProviders();
            logging.AddSimpleConsole(options =>
            {
                options.IncludeScopes = true;
                options.SingleLine = true;
                options.TimestampFormat = "hh:mm:ss ";
            }).SetMinimumLevel(LogLevel.Information);
            logging.AddLogLevelFilters();
            logging.AddApplicationInsights(
                configureTelemetryConfiguration: (config) =>
                    config.ConnectionString = Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING"),
                    configureApplicationInsightsLoggerOptions: (options) => { }
            );
        });
        return services;
    }

Upvotes: 2

Views: 74

Answers (0)

Related Questions