C# Applicationinsights自定义遥测导致ObjectDisposedException
我正在尝试为我们的application insights服务获取已连接用户的用户名,但将自定义遥测添加到日志用户名会给我一个C# Applicationinsights自定义遥测导致ObjectDisposedException,c#,asp.net,.net,azure-application-insights,C#,Asp.net,.net,Azure Application Insights,我正在尝试为我们的application insights服务获取已连接用户的用户名,但将自定义遥测添加到日志用户名会给我一个System.ObjectDisposedException,它发生在mscorlib.dll中,但未在用户代码中处理错误 例外情况: Exception thrown: 'System.ObjectDisposedException' in mscorlib.dll An exception of type 'System.ObjectDisposedException
System.ObjectDisposedException,它发生在mscorlib.dll中,但未在用户代码中处理错误
例外情况:
Exception thrown: 'System.ObjectDisposedException' in mscorlib.dll
An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but was not handled in user code
Safe handle has been closed
堆栈跟踪:
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.Infrastructure.Logging.AppInsightsInitializer.Initialize(ITelemetry telemetry) in C:\Users\SESEGU\Documents\Projects\WebEco\WebEco.Infrastructure\Logging\AppInsightsInitializer.cs:line 22
at Microsoft.ApplicationInsights.TelemetryClient.Initialize(ITelemetry telemetry)
以下是我使用的代码:
public class TelemetryInitializer : ITelemetryInitializer
{
private readonly IHttpContextAccessor _httpContext;
private readonly ILogger<TelemetryInitializer> _logger;
public TelemetryInitializer(IHttpContextAccessor httpContext, ILogger<TelemetryInitializer> logger)
{
_httpContext = httpContext;
_logger = logger;
}
public void Initialize(ITelemetry telemetry)
{
var requestTelemetry = telemetry as RequestTelemetry;
if (requestTelemetry == null) return;
if (!requestTelemetry.Context.GlobalProperties.ContainsKey("UserName"))
{
if (string.IsNullOrEmpty(_httpContext.HttpContext.User.Identity.Name))
{
requestTelemetry.Context.GlobalProperties.Add("UserName", "NA");
}
else
{
requestTelemetry.Context.GlobalProperties.Add("UserName", _httpContext.HttpContext.User.Identity.Name);
}
}
}
}
公共类遥测初始化器:ITelemetryInitializer
{
专用只读IHttpContextAccessor\u httpContext;
专用只读ILogger\u记录器;
公共遥测初始化器(IHttpContextAccessor httpContext,ILogger记录器)
{
_httpContext=httpContext;
_记录器=记录器;
}
公共无效初始化(ITelemetry遥测)
{
var requestTelemetry=作为requestTelemetry的遥测;
if(requestTelemetry==null)返回;
if(!requestTelemetry.Context.GlobalProperties.ContainsKey(“用户名”))
{
if(string.IsNullOrEmpty(_-httpContext.httpContext.User.Identity.Name))
{
requestTelemetry.Context.GlobalProperties.Add(“用户名”,“NA”);
}
其他的
{
requestTelemetry.Context.GlobalProperties.Add(“用户名”,_httpContext.httpContext.User.Identity.Name);
}
}
}
}
有人遇到过类似的问题吗?您是如何解决的?只是想确认一下,这是一个asp.net项目?不是.net core?您没有发布异常文本、调用堆栈或指示错误发生的位置,因此人们只能猜测。每个请求都有自己的HttpContext实例,该实例在请求结束时被销毁。@它的目标是.NET Framework 4.6.2发布完整的异常文本,包括堆栈跟踪。使用Exception.ToString()
可以很容易地获得。我怀疑问题出在调用HttpContext
之后disposed@PanagiotisKanavos我已经在上面添加了完整的stacktrace。