Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Application Insights将用户名添加到遥测_C#_Asp.net_Azure Application Insights - Fatal编程技术网

C# Application Insights将用户名添加到遥测

C# Application Insights将用户名添加到遥测,c#,asp.net,azure-application-insights,C#,Asp.net,Azure Application Insights,当用户执行请求时,我试图在ApplicationInsights中记录用户的用户名 我一直在尝试这样做: public class AppInsightsInitializer : ClientIpHeaderTelemetryInitializer { private readonly IHttpContextAccessor _httpContextAccessor; public AppInsightsInitializer(IHttpConte

当用户执行请求时,我试图在ApplicationInsights中记录用户的用户名

我一直在尝试这样做:

 public class AppInsightsInitializer : ClientIpHeaderTelemetryInitializer
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public AppInsightsInitializer(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
        {
            var userName = _httpContextAccessor.HttpContext?.User?.Identity?.Name; // Only set when request failed...
            var ip = _httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
            if (userName != null) requestTelemetry.Context.User.AuthenticatedUserId= userName;
        }
    }
Startup.cs中注入并注册Teletry后:

 services.AddApplicationInsightsTelemetry(instrumentKey);
 services.AddSingleton<ITelemetryInitializer, AppInsights.AppInsightsInitializer>();
然后我尝试了以下代码:

 public class AppInsightsInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            var identity = WindowsIdentity.GetCurrent();
            if (identity != null)
            {
                var name = new WindowsPrincipal(identity);
                telemetry.Context.User.AuthenticatedUserId = name.Identity.Name;
            }
        }
    }
这在本地计算机上运行良好。但当我在dev环境中尝试时,它只记录服务器名而不是用户名

所以我认为我的第一个解决方案是正确的。但是,我无法通过它崩溃,因为
安全句柄已关闭
异常


有什么帮助吗?

环顾四周后,看起来这可能会解决您的问题

    public class AuthenticatedUserIdTelemetryInitializer : ITelemetryInitializer
    {
        IHttpContextAccessor httpContextAccessor;

        public AuthenticatedUserIdTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }

        public void Initialize(ITelemetry telemetry)
        {
            if (this.httpContextAccessor?.HttpContext?.User?.Identity?.IsAuthenticated == true)
                telemetry.Context.User.AuthenticatedUserId = this.httpContextAccessor.HttpContext.User.Identity.Name;
        }
    }
这实际上是你两个例子的结合。 我认为区别在于使用
Initialize
方法而不是重写的
OnInitializeTelemetry
方法。 可能是您的
httpContextAccessor
在Initialize方法中仍处于活动状态,而它已经在被重写的方法中释放。 不确定从
ClientIpHeaderTelemetryInitializer继承的动机是什么,以及是否相关


代码是从中复制的。

使用上述代码仍然会给我一个安全手柄已关闭。这一个是否幸运?我(终于)找到了一个时间来实际测试这段代码,它按预期(原样)工作,在ApplicationInsights中使用登录用户的实际用户名设置AuthUserID字段。这是新创建的.NETCore2.1MVC项目。
    public class AuthenticatedUserIdTelemetryInitializer : ITelemetryInitializer
    {
        IHttpContextAccessor httpContextAccessor;

        public AuthenticatedUserIdTelemetryInitializer(IHttpContextAccessor httpContextAccessor)
        {
            this.httpContextAccessor = httpContextAccessor;
        }

        public void Initialize(ITelemetry telemetry)
        {
            if (this.httpContextAccessor?.HttpContext?.User?.Identity?.IsAuthenticated == true)
                telemetry.Context.User.AuthenticatedUserId = this.httpContextAccessor.HttpContext.User.Identity.Name;
        }
    }