gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:38:39 25 4
gpt4 key购买 nike

我尝试在用户发出请求时在应用程序洞察中记录用户的用户名。

我一直在尝试这样做:

 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 中注入(inject)并注册 telemtry 后:

 services.AddApplicationInsightsTelemetry(instrumentKey);
services.AddSingleton<ITelemetryInitializer, AppInsights.AppInsightsInitializer>();

但这会导致 System.ObjectDisposeException: '安全句柄已关闭

System.ObjectDisposedException
HResult=0x80131622
Message=Safe handle has been closed
Source=mscorlib
StackTrace:
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Microsoft.Win32.Win32Native.GetTokenInformation(SafeAccessTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeAccessTokenHandle tokenHandle, TokenInformationClass tokenInformationClass)
at System.Security.Principal.WindowsIdentity.get_User()
at System.Security.Principal.WindowsIdentity.GetName()
at System.Security.Principal.WindowsIdentity.get_Name()
at Webeco.Web.AppInsights.AppInsightsInitializer.OnInitializeTelemetry(HttpContext platformContext, RequestTelemetry requestTelemetry, ITelemetry telemetry) in C:\Users\SESEGU\Documents\Projects\AppInsights\AppInsightsInitializer.cs:line 24
at Microsoft.ApplicationInsights.AspNetCore.TelemetryInitializers.TelemetryInitializerBase.Initialize(ITelemetry telemetry)
at Microsoft.ApplicationInsights.TelemetryClient.Initialize(ITelemetry telemetry)

然后我尝试了以下代码:

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

这在本地机器上运行良好。但是当我在开发环境中尝试它时,它只记录服务器名称而不是用户名。

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

有什么帮助可以克服这个问题吗?

最佳答案

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

    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 继承的动机是什么以及是否相关。

代码复制自here .

关于c# - Application Insights 将用户名添加到遥测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59320559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com