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 (#5115)
  • Loading branch information
snakefoot committed Nov 29, 2022
2 parents 5e6708d + cfe8f0e commit dfe19ce
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 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
10 changes: 5 additions & 5 deletions src/NLog/Config/LoggingConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private Target RemoveTargetThreadSafe(string name)

if (target != null)
{
InternalLogger.Debug("Unregistered target {0}(Name={1})", target.GetType().Name, target.Name);
InternalLogger.Debug("Unregistered target {0}(Name={1})", target.GetType(), target.Name);
}

return target;
Expand Down Expand Up @@ -160,11 +160,11 @@ private void AddTargetThreadSafe(Target target, string targetAlias = null)

if (!string.IsNullOrEmpty(target.Name) && !string.Equals(target.Name, targetAlias, StringComparison.OrdinalIgnoreCase))
{
InternalLogger.Info("Registered target {0}(Name={1}) (Extra alias={2})", target.GetType().Name, target.Name, targetAlias);
InternalLogger.Info("Registered target {0}(Name={1}) (Extra alias={2})", target.GetType(), target.Name, targetAlias);
}
else
{
InternalLogger.Info("Registered target {0}(Name={1})", target.GetType().Name, target.Name);
InternalLogger.Info("Registered target {0}(Name={1})", target.GetType(), target.Name);
}
}

Expand Down Expand Up @@ -216,7 +216,7 @@ public void AddTarget([NotNull] Target target)
{
if (target is null) { throw new ArgumentNullException(nameof(target)); }

InternalLogger.Debug("Adding target {0}(Name={1})", target.GetType().Name, target.Name);
InternalLogger.Debug("Adding target {0}(Name={1})", target.GetType(), target.Name);

if (string.IsNullOrEmpty(target.Name)) { throw new ArgumentException(nameof(target) + ".Name cannot be empty", nameof(target)); }

Expand All @@ -235,7 +235,7 @@ public void AddTarget(string name, [NotNull] Target target)
if (name is null) { throw new ArgumentNullException(nameof(name)); }
if (target is null) { throw new ArgumentNullException(nameof(target)); }

InternalLogger.Debug("Adding target {0}(Name={1})", target.GetType().Name, string.IsNullOrEmpty(name) ? target.Name : name);
InternalLogger.Debug("Adding target {0}(Name={1})", target.GetType(), string.IsNullOrEmpty(name) ? target.Name : name);

if (string.IsNullOrEmpty(name)) { throw new ArgumentException("Target name cannot be empty", nameof(name)); }

Expand Down
2 changes: 1 addition & 1 deletion src/NLog/Config/LoggingConfigurationParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,7 @@ private Target WrapWithDefaultWrapper(Target target, ValidatedConfigurationEleme
target.Name = target.Name + "_wrapped";

InternalLogger.Debug("Wrapping target '{0}' with '{1}' and renaming to '{2}", wrapperTargetInstance.Name,
wrapperTargetInstance.GetType().Name, target.Name);
wrapperTargetInstance.GetType(), target.Name);
return wrapperTargetInstance;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NLog/Config/MethodFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void ScanTypes(Type[] types, string assemblyName, string itemNamePrefix)
{
try
{
if (t.IsClass() || t.IsAbstract())
if (t.IsClass() && t.IsPublic())
{
RegisterType(t, assemblyName, itemNamePrefix);
}
Expand Down
18 changes: 10 additions & 8 deletions src/NLog/Internal/Reflection/PropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ namespace NLog.Internal
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using NLog.Common;
using NLog.Conditions;
using NLog.Config;
using NLog.Internal;
using NLog.Layouts;
using NLog.Targets;

Expand Down Expand Up @@ -92,7 +90,6 @@ internal static class PropertyHelper
internal static void SetPropertyFromString(object targetObject, string propertyName, string stringValue, ConfigurationItemFactory configurationItemFactory)
{
var objType = targetObject.GetType();
InternalLogger.Debug("Setting '{0}.{1}' to '{2}'", objType, propertyName, stringValue);

if (!TryGetPropertyInfo(objType, propertyName, out var propInfo))
{
Expand All @@ -106,6 +103,8 @@ internal static void SetPropertyFromString(object targetObject, PropertyInfo pro
{
object propertyValue = null;

InternalLogger.Debug("Setting '{0}.{1}' to '{2}'", targetObject?.GetType(), propInfo.Name, stringValue);

try
{
var propertyType = Nullable.GetUnderlyingType(propInfo.PropertyType) ?? propInfo.PropertyType;
Expand Down Expand Up @@ -223,13 +222,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
2 changes: 1 addition & 1 deletion src/NLog/Layouts/LayoutParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ private static string SetDefaultPropertyValue(string value, LayoutRenderer layou
value = EscapeUnicodeStringValue(value);
}

PropertyHelper.SetPropertyFromString(layoutRenderer, propertyInfo.Name, value, configurationItemFactory);
PropertyHelper.SetPropertyFromString(layoutRenderer, propertyInfo, value, configurationItemFactory);
return propertyInfo.Name;
}
else
Expand Down

0 comments on commit dfe19ce

Please sign in to comment.