When I try to log the user session using the steps from this post: Send user context IDs to enable usage experiences in Azure Application Insights in webforms, the information is not available in Azure Application Insights
Repro Steps 1. Add Telemetry Inicializer
public class TelemetryInitializer : ITelemetryInitializer
{
public void Initialize(ITelemetry telemetry)
{
var ctx = HttpContext.Current;
// If telemetry initializer is called as part of request execution and not from some async thread
if (ctx != null)
{
var requestTelemetry = ctx.GetRequestTelemetry();
// Set the user and session ids from requestTelemetry.Context.User.Id, which is populated in Application_PostAcquireRequestState in Global.asax.cs.
if (requestTelemetry != null && !string.IsNullOrEmpty(requestTelemetry.Context.User.Id) &&
(string.IsNullOrEmpty(telemetry.Context.User.Id) || string.IsNullOrEmpty(telemetry.Context.Session.Id)))
{
// Set the user id on the Application Insights telemetry item.
telemetry.Context.User.Id = requestTelemetry.Context.User.Id;
// Set the session id on the Application Insights telemetry item.
telemetry.Context.Session.Id = requestTelemetry.Context.User.Id;
}
}
}
}
- Register the Initializer at Global.asax
TelemetryConfiguration.Active.TelemetryInitializers.Add(new ManageTelemetryInitializer());
- Fill the data at Application_PostAcquireRequestState
protected void Application_PostAcquireRequestState()
{
var requestTelemetry = Context.GetRequestTelemetry();
if (HttpContext.Current.Session != null && requestTelemetry != null && string.IsNullOrEmpty(requestTelemetry.Context.User.Id))
{
UserSession userSession = // GetMySession
string userId = string.Empty;
if (userSession != null)
{
userId = userSession.UserGuid.ToString("N");
}
requestTelemetry.Context.User.Id = userId;
requestTelemetry.Context.Session.Id = Session.SessionID;
}
}
Actual Behavior Session data is not available in Azure Application Insights
Expected Behavior Having the correct user session in Azure Application Insights
UPDATE:
Here's the data which I can see on the output window when I debug. Everything looks fine. I omitted some fields because of privacy.
{
"name": "Microsoft.ApplicationInsights.Dev.fc9e8309cdd74395bf57f81d63d915d9.Message",
"time": "2019-10-18T10:13:21.0548760Z",
"sampleRate": 33.3333333333333,
"iKey": "INSTRUMENTATION_KEY",
"tags": {
"ai.internal.sdkVersion": "sd:2.4.1-442",
"ai.session.id": "oaluibbilrmunjxc3i1zovld",
"ai.operation.id": "OPERATION_ID",
"ai.location.ip": "::1",
"ai.cloud.roleInstance": "MACHINE_NAME",
"ai.user.id": "cef5121344374f25a325a1079473c51a",
"ai.operation.name": "OEPRATION",
"ai.operation.parentId": "PARENT_ID"
},
"data": {
"baseType": "MessageData",
"baseData": {
"ver": 2,
"message": "MESSAGE",
"severityLevel": "Verbose",
"properties": {
"DeveloperMode": "true"
}
}
}
}
userId
andSession.SessionID
set when theApplication_PostAcquireRequestState
is called ? – Pietrosession_Id
anduser_Id
in Azure Application Insights. I am using the implementation you provided in your question as a link to ms page. – Pietro