1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyLibrary.Web.Context;
using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.Extensibility;


namespace ACME.Foundation.Analytics
{
    public class ApplicationInsightsSessionInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var requestTelemetry = telemetry as RequestTelemetry;
            if (requestTelemetry == null) return;

            if (HttpContext.Current.Session.SessionID != null)
            {
                telemetry.Context.Session.Id = HttpContext.Current.Session.SessionID;
            }
        }
    }
}

In ApplicationInsights.config

<Add Type="ACME.Foundation.Analytics.ApplicationInsightsSessionInitializer, ACME.Foundation.Analytics" />
<Add Type="Microsoft.ApplicationInsights.StatusMonitor.SdkSourceTelemetryInitializer, Microsoft.AI.StatusMonitor" />

How do I get session id from requestTelemetry? I have this snippet in my analytics telemetry folder. It seems as if the itemType Request does not have a a session ID. Everytime a request appears, the session is null.

I'd like to match it with the proper sessionID, and can't override it in the app insights sessionID

I was following the example here: https://learn.microsoft.com/en-us/azure/application-insights/app-insights-usage-send-user-context

and based my solution on the demo located halfway down the page, but doesn't seem to be working for me.

Any help would be appreciated!

Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
dan
  • 13
  • 5
  • In short, this example is incorrect and there is a [documentation issue](https://github.com/MicrosoftDocs/azure-docs/issues/11721) opened against it on GitHub and a [user voice](https://feedback.azure.com/forums/357324-application-insights/suggestions/32508511-fix-the-session-examples-or-app-insights-behaviour) item on Azure Feedback. There are several suggestions / workarounds listed in those issues that may be of help. – Dmitry Matveev Sep 13 '18 at 00:22

1 Answers1

0

as per @Dmitry metioned, the example is incorrect.

There is another way you can set sessionid, you can refer to this link:

private static TelemetryClient GetTelemetryClient()
{
var telemetryClient = new TelemetryClient();
telemetryClient.InstrumentationKey = "<your actual insight instrumentkey>";
telemetryClient.Context.Session.Id = "124556";
//update 1-Custom properties- Start
telemetry.Context.Properties["tags"] = "PROD";
telemetry.Context.Properties["userCorelateId"]="1234"; 
//update 1-Custom properties- Ends                
return telemetryClient;
} 
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60