Skip to content

Commit

Permalink
ConfigurationItemFactory - Faster scan of NLog types with filter on I…
Browse files Browse the repository at this point in the history
…sPublic / IsClass
  • Loading branch information
snakefoot committed Nov 28, 2022
1 parent 5e6708d commit 084d03a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/NLog/Config/ConfigurationItemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,18 @@ public void RegisterItemsFromAssembly(Assembly assembly, string itemNamePrefix)
var typesToScan = assembly.SafeGetTypes();
if (typesToScan?.Length > 0)
{
var assemblyName = new AssemblyName(assembly.FullName).Name;
PreloadAssembly(typesToScan);
string assemblyName = string.Empty;

if (ReferenceEquals(assembly, typeof(LogFactory).GetAssembly()))
{
typesToScan = typesToScan.Where(t => t.IsPublic() && t.IsClass()).ToArray();
}
else
{
assemblyName = new AssemblyName(assembly.FullName).Name;
PreloadAssembly(typesToScan);
}

foreach (IFactory f in _allFactories)
{
f.ScanTypes(typesToScan, assemblyName, itemNamePrefix);
Expand Down
13 changes: 8 additions & 5 deletions src/NLog/Internal/Reflection/PropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,16 @@ internal static void CheckRequiredParameters(object o)
foreach (var configProp in GetAllConfigItemProperties(o.GetType()))
{
var propInfo = configProp.Value;
if (propInfo.IsDefined(_requiredParameterAttribute.GetType(), false))
if (propInfo.PropertyType?.IsClass() == true)
{
object value = propInfo.GetValue(o, null);
if (value is null)
if (propInfo.IsDefined(_requiredParameterAttribute.GetType(), false))
{
throw new NLogConfigurationException(
$"Required parameter '{propInfo.Name}' on '{o}' was not specified.");
object value = propInfo.GetValue(o, null);
if (value is null)
{
throw new NLogConfigurationException(
$"Required parameter '{propInfo.Name}' on '{o}' was not specified.");
}
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/NLog/Internal/Reflection/ReflectionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ private static UnaryExpression CreateParameterExpression(ParameterInfo parameter
return valueCast;
}

public static bool IsPublic(this Type type)
{
#if !NETSTANDARD1_3 && !NETSTANDARD1_5
return type.IsPublic;
#else
return type.GetTypeInfo().IsPublic;
#endif
}

public static bool IsEnum(this Type type)
{
#if !NETSTANDARD1_3 && !NETSTANDARD1_5
Expand Down

0 comments on commit 084d03a

Please sign in to comment.