1
Vote

TraceEvent GetProviderName NullReferenceException

description

Calling TraceEventSession.GetProviderName always results in a NullReferenceException. That is because the getter of the ProviderGuidToName property fails to create the dictionary before filling it.
    private static Dictionary<Guid, string> ProviderGuidToName
    {
        get
        {
            if (s_providerNames == null)
            {
                foreach (var keyValue in ProviderNameToGuid)
                    s_providerNames[keyValue.Value] = keyValue.Key; // NullReferenceException
            }
            return s_providerNames;
        }
    }
The fix is simple. Create the dictionary before the foreach
    private static Dictionary<Guid, string> ProviderGuidToName
    {
        get
        {
            if (s_providerNames == null)
            {
                s_providerNames = new Dictionary<Guid, string>(); // FIX
                foreach (var keyValue in ProviderNameToGuid)
                    s_providerNames[keyValue.Value] = keyValue.Key;
            }
            return s_providerNames;
        }
    }

comments