Improved logging when attempting to cache an assembly

This commit is contained in:
Chris Cameron
2017-10-16 10:58:03 -04:00
parent d8ab73d356
commit 11f3f480b0

View File

@@ -27,6 +27,8 @@ namespace ICD.Common.Utils
private static readonly Dictionary<Attribute, MethodInfo> s_AttributeToMethodCache; private static readonly Dictionary<Attribute, MethodInfo> s_AttributeToMethodCache;
private static readonly Dictionary<Type, IcdHashSet<Attribute>> s_TypeToAttributesCache; private static readonly Dictionary<Type, IcdHashSet<Attribute>> s_TypeToAttributesCache;
private static ILoggerService Logger { get { return ServiceProvider.TryGetService<ILoggerService>(); } }
/// <summary> /// <summary>
/// Constructor. /// Constructor.
/// </summary> /// </summary>
@@ -58,33 +60,52 @@ namespace ICD.Common.Utils
/// Pre-emptively caches the given assembly for lookup. /// Pre-emptively caches the given assembly for lookup.
/// </summary> /// </summary>
/// <param name="assembly"></param> /// <param name="assembly"></param>
public static void CacheAssembly(Assembly assembly) public static bool CacheAssembly(Assembly assembly)
{ {
if (assembly == null) if (assembly == null)
throw new ArgumentNullException("assembly"); throw new ArgumentNullException("assembly");
if (s_CachedAssemblies.Contains(assembly)) if (s_CachedAssemblies.Contains(assembly))
return; return true;
s_CachedAssemblies.Add(assembly);
#if SIMPLSHARP #if SIMPLSHARP
CType[] types = new CType[0]; CType[] types;
#else #else
Type[] types = new Type[0]; Type[] types;
#endif #endif
try try
{ {
types = assembly.GetTypes(); types = assembly.GetTypes();
} }
#if STANDARD
catch (ReflectionTypeLoadException e)
{
foreach (Exception inner in e.LoaderExceptions)
{
Logger.AddEntry(eSeverity.Error, inner, "{0} failed to cache assembly {1}", typeof(AttributeUtils).Name,
assembly.GetName().Name);
}
return false;
}
#endif
catch (TypeLoadException e) catch (TypeLoadException e)
{ {
ServiceProvider.TryGetService<ILoggerService>() #if SIMPLSHARP
.AddEntry(eSeverity.Error, e, "Failed to cache assembly {0} - {1}", assembly.GetName().Name, Logger.AddEntry(eSeverity.Error, e, "{0} failed to cache assembly {1}", typeof(AttributeUtils).Name,
e.Message); assembly.GetName().Name);
#else
Logger.AddEntry(eSeverity.Error, e, "{0} failed to cache assembly {1} - could not load type {2}",
typeof(AttributeUtils).Name, assembly.GetName().Name, e.TypeName);
#endif
return false;
} }
foreach (var type in types) foreach (var type in types)
CacheType(type); CacheType(type);
s_CachedAssemblies.Add(assembly);
return true;
} }
/// <summary> /// <summary>
@@ -145,9 +166,9 @@ namespace ICD.Common.Utils
s_AttributeToMethodCache[attribute] = method; s_AttributeToMethodCache[attribute] = method;
} }
#endregion #endregion
#region Lookup #region Lookup
/// <summary> /// <summary>
/// Gets the first attribute on the given class type matching the generic type. /// Gets the first attribute on the given class type matching the generic type.
@@ -223,6 +244,6 @@ namespace ICD.Common.Utils
return s_AttributeToMethodCache[attribute]; return s_AttributeToMethodCache[attribute];
} }
#endregion #endregion
} }
} }