From 463c9679c0d7e92266a4cff21fff56c35b8dca0b Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Mon, 28 Nov 2022 21:01:54 +0100 Subject: [PATCH 1/2] ConfigurationItemFactory - Faster scan of NLog types with filter on IsPublic / IsClass --- src/NLog/Config/ConfigurationItemFactory.cs | 14 ++++++++++++-- src/NLog/Config/MethodFactory.cs | 2 +- src/NLog/Internal/Reflection/PropertyHelper.cs | 13 ++++++++----- src/NLog/Internal/Reflection/ReflectionHelpers.cs | 9 +++++++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/NLog/Config/ConfigurationItemFactory.cs b/src/NLog/Config/ConfigurationItemFactory.cs index fc48a9260d..dc4be4c934 100644 --- a/src/NLog/Config/ConfigurationItemFactory.cs +++ b/src/NLog/Config/ConfigurationItemFactory.cs @@ -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); diff --git a/src/NLog/Config/MethodFactory.cs b/src/NLog/Config/MethodFactory.cs index aae1e89923..f1e62c7ad9 100644 --- a/src/NLog/Config/MethodFactory.cs +++ b/src/NLog/Config/MethodFactory.cs @@ -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); } diff --git a/src/NLog/Internal/Reflection/PropertyHelper.cs b/src/NLog/Internal/Reflection/PropertyHelper.cs index a7ba08d0d7..ba4df66c15 100644 --- a/src/NLog/Internal/Reflection/PropertyHelper.cs +++ b/src/NLog/Internal/Reflection/PropertyHelper.cs @@ -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."); + } } } } diff --git a/src/NLog/Internal/Reflection/ReflectionHelpers.cs b/src/NLog/Internal/Reflection/ReflectionHelpers.cs index 707caeeeaa..419d204cee 100644 --- a/src/NLog/Internal/Reflection/ReflectionHelpers.cs +++ b/src/NLog/Internal/Reflection/ReflectionHelpers.cs @@ -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 From cfe8f0e060d9d50fbc59de2d7b71db8fc1fdca73 Mon Sep 17 00:00:00 2001 From: Rolf Kristensen Date: Tue, 29 Nov 2022 07:51:50 +0100 Subject: [PATCH 2/2] LayoutParser - Improve output to InternalLogger when setting property-values --- src/NLog/Config/LoggingConfiguration.cs | 10 +++++----- src/NLog/Config/LoggingConfigurationParser.cs | 2 +- src/NLog/Internal/Reflection/PropertyHelper.cs | 5 ++--- src/NLog/Layouts/LayoutParser.cs | 2 +- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/NLog/Config/LoggingConfiguration.cs b/src/NLog/Config/LoggingConfiguration.cs index c526d43f08..9d876ca3df 100644 --- a/src/NLog/Config/LoggingConfiguration.cs +++ b/src/NLog/Config/LoggingConfiguration.cs @@ -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; @@ -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); } } @@ -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)); } @@ -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)); } diff --git a/src/NLog/Config/LoggingConfigurationParser.cs b/src/NLog/Config/LoggingConfigurationParser.cs index d583d8df84..f7cfa6611c 100644 --- a/src/NLog/Config/LoggingConfigurationParser.cs +++ b/src/NLog/Config/LoggingConfigurationParser.cs @@ -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; } diff --git a/src/NLog/Internal/Reflection/PropertyHelper.cs b/src/NLog/Internal/Reflection/PropertyHelper.cs index ba4df66c15..ba69960381 100644 --- a/src/NLog/Internal/Reflection/PropertyHelper.cs +++ b/src/NLog/Internal/Reflection/PropertyHelper.cs @@ -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; @@ -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)) { @@ -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; diff --git a/src/NLog/Layouts/LayoutParser.cs b/src/NLog/Layouts/LayoutParser.cs index 04982bc677..efd9eda0b8 100644 --- a/src/NLog/Layouts/LayoutParser.cs +++ b/src/NLog/Layouts/LayoutParser.cs @@ -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