Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting username in application insights

I am new to application insights and have set it up using no custom events and I'm using all the defaults. The application is build on MVC 5. In the ApplicationInsights.config there's a comment saying:

"When implementing custom user tracking in your application, remove this telemetry initializer to ensure that the number of users is accurately reported to Application Insights."

We have a page where you are required to login so the default user logging isn't saying that much and we would much rather have the username being the unique identifier. Based on the comment it seems like this should be some kind of common modification and therefor easy to modify. When trying to google on "custom user tracking" I do not find anything interesting which seems a bit odd...

So how do I link the user in Application Insights to my username instead of going on some cookie which seems to be the default behaviour?

like image 963
user1842278 Avatar asked May 25 '15 09:05

user1842278


People also ask

What is application Insights config?

The Application Insights . NET SDK consists of many NuGet packages. The core package provides the API for sending telemetry to the Application Insights. More packages provide telemetry modules and initializers for automatically tracking telemetry from your application and its context.

How do I change log analytics workspace in application Insights?

Once a workspace-based Application Insights resource has been created, you can modify the associated Log Analytics Workspace. From within the Application Insights resource pane, select Properties > Change Workspace > Log Analytics Workspaces.

Can user turn off app insight?

To turn off Application Insights, remove the module from the System. WebServer/Modules section of Web. config. If you want to remove it altogether, though, you should uninstall the Application Insight NuGet packages, and delete ApplicationInsights.


1 Answers

To link the user to your custom username, 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 AI.config.

      <TelemetryInitializers>
 ....
        <Add Type="MyApp.RealUserIDTelemetryInitializer, MyApp" />
      </TelemetryInitializers>
like image 169
Alex Bulankou Avatar answered Sep 22 '22 18:09

Alex Bulankou