ApplicationInsights-dotnet icon indicating copy to clipboard operation
ApplicationInsights-dotnet copied to clipboard

Linking telemetry in a .NET Framework Console App

Open shiningdragon opened this issue 5 years ago • 2 comments

I am using app insights in a .NET Framework Console app

It is not possible for me to use Microsoft.ApplicationInsights.WorkerService so instead I am following this documentation https://docs.microsoft.com/en-us/azure/azure-monitor/app/console

I can not find any way to link telemetry using operationid/parentid etc

A simple version of the code is as follows

    class Program
    {
        static void Main(string[] args)
        {
            TelemetryConfiguration configuration = TelemetryConfiguration.CreateDefault();
            configuration.InstrumentationKey = "xxxxxxxxxxxxxxxxxxxxxxx";
            var telemetryClient = new TelemetryClient(configuration);

            telemetryClient.TrackEvent("Example event");
            telemetryClient.TrackTrace("Example trace");

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

I would like the custom event and the trace linked together. I have tried using OperationCorrelationTelemetryInitializer both from within code and from ApplicationInsights.config but the event and trace don't seem to be logged with any operationid or parentid.

Is telemetry linking possible in this scenario and if so can you provide sample code?

shiningdragon avatar Mar 01 '21 12:03 shiningdragon

The events must be under a common operation to be correlated. One example of achieving it is shown below.

using (telemetryClient.StartOperation<RequestTelemetry>("operation"))
                    {                        
                        telemetryClient.TrackEvent("Event1");
                        telemetryClient.TrackEvent("Event2");
                    }

cijothomas avatar Mar 02 '21 23:03 cijothomas

yes this works and looks promising, thankyou.

shiningdragon avatar Mar 03 '21 15:03 shiningdragon