Applicationinsights customized telemetry leads to ObjectDisposedException

0

I'm trying to get the UserName of connected users for our application insights services but adding the custom telemetry to log UserName gives me a System.ObjectDisposedException occurred in mscorlib.dll but was not handled in user code error.

Exception:

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

StackTrace:

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)

Here's the code I'm using:

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

Anyone ran into similar problems and how did you fix it?

c#
asp.net
.net
azure-application-insights
asked on Stack Overflow Dec 4, 2019 by Gurkmeja101 • edited Dec 4, 2019 by Gurkmeja101

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0