从ASP.NET Core填充Application Insights中的用户ID字段 [英] Filling User Id Field in Application Insights from ASP.NET Core

查看:163
本文介绍了从ASP.NET Core填充Application Insights中的用户ID字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够用我的真实用户名数据填充Application Insights中的用户ID"字段.这是一个内部应用程序,因此对于带有简单用户名字段的隐私问题没有任何争议.

I would like to be able to populate the User Id field in Application Insights with my real username data. This is an internal application, so privacy concerns with a simple username field are moot.

据我所知,与此相关的所有在线可用解决方案都严格在.NET Framework(而不是.NET Core)中工作.

As far as I can tell, all solutions available online for this strictly work in .NET Framework, not .NET Core.

您可以找到此解决方案在几个地方,包括GitHub上的一些旧AI文档.但是,当我运行它时,我在启动时收到错误消息,表明从单例来看,对范围对象IHttpContextAccessor的依赖是不可接受的,这当然是合乎逻辑的.我不知道这是如何工作的,除非.NET Core的DI的早期版本允许它(我使用的是2.2版).

You can find this solution in a few places, including some old AI documentation on GitHub. However, when I run it, I get an error on startup indicating that dependency on the scoped object IHttpContextAccessor is not acceptable from a singleton, which of course is logical. I don't see how this could have ever worked unless a previous version of .NET Core's DI allowed it (I'm on 2.2).

在GitHub上出现的问题可以清楚地说明问题所在,但是在AI团队指出您必须使用单例之后关闭.我尝试了OP中的内容和第一个响应的变体,并且在代码运行时,AI的User Id字段继续充满了乱码.

This issue on GitHub sort of spells out the problem but it was closed after the AI team pointed out that you must use a singleton. I tried variations of what's in the OP and the first response, and while the code ran, the User Id field on AI continued to be filled with gibberish data.

有什么方法可以使AI中的用户ID"字段填充一些对来自ASP.NET Core应用程序的服务器请求有用的信息吗?

Is there any way to make the User Id field in AI fill with something useful for server requests coming from an ASP.NET Core app?

看到下面的答案我的代码应该可以正常工作后,我回过头来发现异常消息没有特别提到IHttpContextAccessor:

After seeing the answer below that my code should have worked just fine, I went back and realized the exception message hadn't specifically mentioned IHttpContextAccessor:

System.InvalidOperationException:'无法使用作用域服务 'Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer' 从单身人士 'Microsoft.Extensions.Options.IConfigureOptions`1 [Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]'.'

System.InvalidOperationException: 'Cannot consume scoped service 'Microsoft.ApplicationInsights.Extensibility.ITelemetryInitializer' from singleton 'Microsoft.Extensions.Options.IConfigureOptions`1[Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration]'.'

现在,我的代码看起来与下面的@PeterBons的答案几乎相同,因此从表面上看,此异常没有任何意义. TelemetryConfiguration没有事件出现在我的代码中.但是后来我想起我正在使用Scrutor懒惰地在Startup中进行DI注册:

Now, my code looks just about identical to @PeterBons' answer below, so on the face of it this exception made no sense. TelemetryConfiguration doesn't event appear in my code. But then I remembered I am using Scrutor to lazily do DI registration in Startup:

    services.Scan(scan => scan
        .FromAssembliesOf(typeof(Startup), typeof(MyDbContext))
        .AddClasses()
        .AsSelfWithInterfaces().WithScopedLifetime());

我认为问题是我需要将ITelemetryInitializer注册为Singleton,并且该代码无意中取消了注册或重新注册为作用域.所以我将最后两行更改为:

I assume the problem was that I need to register my ITelemetryInitializer as Singleton, and this code was inadvertently de- or re-registering it as scoped. So I changed the last two lines to:

        .AddClasses(f => f.Where(t => t != typeof(RealUserAIProvider)))
        .AsSelfWithInterfaces().WithScopedLifetime());

它奏效了.与其在上面编辑我的错误,不如将其保留. @PeterBons在下面的回答仍将对其他人有所帮助,也许我对Scrutor的困惑也将对某人有所帮助.

And it worked. Rather than editing out my mistake above, I'll leave it. @PeterBons' answer below is still going to be helpful to other people, and maybe my confusion with Scrutor will help someone too.

推荐答案

您不必将HttpContextAccessor注册为范围内的依赖项.只需使用一个单例.

You don't have to register the HttpContextAccessor as a scoped dependency. Just use a singleton.

我们正在使用以下方法进行生产:

We have this working in production using this:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

与此初始化程序结合:

public class SessionDetailsTelemetryEnrichment : TelemetryInitializerBase
{
    public SessionDetailsTelemetryEnrichment(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
    {

    }

    protected override void OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry)
    {
        telemetry.Context.User.AuthenticatedUserId =
            platformContext.User?.Claims.FirstOrDefault(c => c.Type == "Username")?.Value ?? string.Empty;

    }
}

这篇关于从ASP.NET Core填充Application Insights中的用户ID字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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