Set username in applications

I am new to application knowledge and configured it without using special events, and I use all the defaults. The application is built on MVC 5. There is a comment in ApplicationInsights.config:

"When implementing custom tracking in your application, remove this telemetry initializer to ensure the exact number of user messages in Application Insights."

We have a page where you need to log in, so registering users by default doesn’t say much, and we would rather prefer the username to be a unique identifier. Based on the commentary, it seems that this should be some kind of general modification, and therefore it is easy to modify it. When I try Google on "user tracking of users" I find nothing interesting that seems a little strange ...

So, how do I associate a user in Application Insights with my username instead of navigating to some cookie that seems to be the default?

+5
source share
1 answer

To associate a user with your user name, you can create the following telemetry initializer:

public class RealUserIDTelemetryInitializer:ITelemetryInitializer { public void Initialize(Microsoft.ApplicationInsights.Channel.ITelemetry telemetry) { // Replace this with your custom logic if (DateTime.Now.Ticks % 2 > 0) { telemetry.Context.User.Id = "Ron Weasley"; } else { telemetry.Context.User.Id = "Hermione Granger"; } } } 

Then register this telemetry initializer in the AI.config file.

  <TelemetryInitializers> .... <Add Type="MyApp.RealUserIDTelemetryInitializer, MyApp" /> </TelemetryInitializers> 
+9
source

All Articles