在应用程序见解中设置用户名 [英] Setting username in application insights

查看:109
本文介绍了在应用程序见解中设置用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是应用程序见解的新手,没有使用任何自定义事件进行设置,而是使用所有默认设置.该应用程序基于MVC 5构建.在ApplicationInsights.config中,有一条注释:

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:

在应用程序中实现自定义用户跟踪时,请删除此遥测初始化程序,以确保将用户数量准确地报告给Application Insights."

"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."

我们有一个需要登录的页面,因此默认用户登录没说太多,我们宁愿使用用户名作为唯一标识符.根据评论,似乎这应该是某种常见的修改,因此很容易修改.当尝试使用Google进行自定义用户跟踪"时,我没有发现任何有趣的东西,似乎有点奇怪...

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...

那么我该如何将Application Insights中的用户链接到我的用户名,而不是使用一些似乎是默认行为的cookie?

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?

推荐答案

要将用户链接到您的自定义用户名,您可以创建以下遥测初始化程序:

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";
        }
    }
}

然后在AI.config中注册此遥测初始化程序.

Then register this telemetry initializer in AI.config.

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

这篇关于在应用程序见解中设置用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆