Fixed from testing
authorVance Morrison <vancemorrison@comcast.net>
Tue, 31 Jul 2018 18:24:18 +0000 (11:24 -0700)
committerVance Morrison <vancemorrison@comcast.net>
Tue, 31 Jul 2018 18:24:18 +0000 (11:24 -0700)
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventProvider.cs
src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs

index 21c2ea0..36b363f 100644 (file)
@@ -883,25 +883,18 @@ namespace System.Diagnostics.Tracing
             {
                 if (data is System.Enum)
                 {
-                    Type underlyingType = Enum.GetUnderlyingType(data.GetType());
-                    if (underlyingType == typeof(int))
+                    try
                     {
-#if !ES_BUILD_PCL
-                        data = ((IConvertible)data).ToInt32(null);
-#else
-                        data = (int)data;
-#endif
-                        goto Again;
-                    }
-                    else if (underlyingType == typeof(long))
-                    {
-#if !ES_BUILD_PCL
-                        data = ((IConvertible)data).ToInt64(null);
-#else
-                        data = (long)data;
-#endif
+                        Type underlyingType = Enum.GetUnderlyingType(data.GetType());
+                        if (underlyingType == typeof(ulong))
+                            data = (ulong)data;
+                        else if (underlyingType == typeof(long))
+                            data = (long)data;
+                        else
+                            data = (int)Convert.ToInt64(data);  // This handles all int/uint or below (we treat them like 32 bit ints)   
                         goto Again;
                     }
+                    catch { }   // On wierd cases (e.g. enums of type double), give up and for compat simply tostring.  
                 }
 
                 // To our eyes, everything else is a just a string
index 1981dc6..4670cc6 100644 (file)
@@ -5451,45 +5451,43 @@ namespace System.Diagnostics.Tracing
                 {
                     bool isbitmap = EventSource.GetCustomAttributeHelper(enumType, typeof(FlagsAttribute), flags) != null;
                     string mapKind = isbitmap ? "bitMap" : "valueMap";
+                    sb.Append("  <").Append(mapKind).Append(" name=\"").Append(enumType.Name).Append("\">").AppendLine();
 
                     // write out each enum value 
                     FieldInfo[] staticFields = enumType.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Static);
-
-                    // Empty map entries cause Windows to reject the whole manifest.  Avoid this by only writting the 
-                    // map header and trailer if we have a valid value entry.  
-                    bool headerWriten = false;
+                    bool anyValuesWritten = false;
                     foreach (FieldInfo staticField in staticFields)
                     {
                         object constantValObj = staticField.GetRawConstantValue();
 
                         if (constantValObj != null)
                         {
-                            try
-                            {
-                                long hexValue = hexValue = Convert.ToInt64(constantValObj); // Handles all integer types.
-
-                                // ETW requires all bitmap values to be powers of 2.  Skip the ones that are not. 
-                                // TODO: Warn people about the dropping of values. 
-                                if (isbitmap && ((hexValue & (hexValue - 1)) != 0 || hexValue == 0))
-                                    continue;
-
-                                // We have a valid value, write the header if we have not already.  
-                                if (!headerWriten)
-                                {
-                                    sb.Append("  <").Append(mapKind).Append(" name=\"").Append(enumType.Name).Append("\">").AppendLine();
-                                    headerWriten = true;
-                                }
-
-                                sb.Append("   <map value=\"0x").Append(hexValue.ToString("x", CultureInfo.InvariantCulture)).Append("\"");
-                                WriteMessageAttrib(sb, "map", enumType.Name + "." + staticField.Name, staticField.Name);
-                                sb.Append("/>").AppendLine();
-                            }
-                            catch { }  
+                            ulong hexValue;
+                            if (constantValObj is ulong)
+                                hexValue = (ulong)constantValObj;    // This is the only integer type that can't be represented by a long.  
+                            else 
+                                hexValue = (ulong) Convert.ToInt64(constantValObj); // Handles all integer types except ulong.  
+
+                            // ETW requires all bitmap values to be powers of 2.  Skip the ones that are not. 
+                            // TODO: Warn people about the dropping of values. 
+                            if (isbitmap && ((hexValue & (hexValue - 1)) != 0 || hexValue == 0))
+                                continue;
+                            sb.Append("   <map value=\"0x").Append(hexValue.ToString("x", CultureInfo.InvariantCulture)).Append("\"");
+                            WriteMessageAttrib(sb, "map", enumType.Name + "." + staticField.Name, staticField.Name);
+                            sb.Append("/>").AppendLine();
+                            anyValuesWritten = true;
                         }
                     }
 
-                    if (headerWriten)
-                        sb.Append("  </").Append(mapKind).Append(">").AppendLine();
+                    // the OS requrires that bitmaps and valuemaps have at least one value or it reject the whole manifest.
+                    // To avoid that put a 'None' entry if there are no other values.  
+                    if (!anyValuesWritten)
+                    {
+                        sb.Append("   <map value=\"0x0\"");
+                        WriteMessageAttrib(sb, "map", enumType.Name + "." + "None", "None");
+                        sb.Append("/>").AppendLine();
+                    }
+                    sb.Append("  </").Append(mapKind).Append(">").AppendLine();
                 }
                 sb.Append(" </maps>").AppendLine();
             }