internal static EnumerableExpansion GetEnumerableExpansionFromType(PSPropertyExpressionFactory expressionFactory, TypeInfoDataBase db, Collection <string> typeNames) { TypeMatch match = new TypeMatch(expressionFactory, db, typeNames); foreach (EnumerableExpansionDirective expansionDirective in db.defaultSettingsSection.enumerableExpansionDirectiveList) { if (match.PerfectMatch(new TypeMatchItem(expansionDirective, expansionDirective.appliesTo))) { return(expansionDirective.enumerableExpansion); } } if (match.BestMatch != null) { return(((EnumerableExpansionDirective)(match.BestMatch)).enumerableExpansion); } else { Collection <string> typesWithoutPrefix = Deserializer.MaskDeserializationPrefix(typeNames); if (typesWithoutPrefix != null) { EnumerableExpansion result = GetEnumerableExpansionFromType(expressionFactory, db, typesWithoutPrefix); return(result); } // return a default value if no matches were found return(EnumerableExpansion.EnumOnly); } }
/// <summary> /// Execution entry point. /// </summary> internal override void ProcessRecord() { _typeInfoDataBase = this.OuterCmdlet().Context.FormatDBManager.GetTypeInfoDataBase(); PSObject so = this.ReadObject(); if (so is null || so == AutomationNull.Value) { return; } IEnumerable e = PSObjectHelper.GetEnumerable(so); if (e is null) { ProcessObject(so); return; } // we have an IEnumerable, we have to decide if to expand, if at all EnumerableExpansion expansionState = this.GetExpansionState(so); switch (expansionState) { case EnumerableExpansion.EnumOnly: { foreach (object obj in e) { ProcessObject(PSObjectHelper.AsPSObject(obj)); } } break; case EnumerableExpansion.Both: { var objs = e.Cast <object>().ToArray(); ProcessCoreOutOfBand(so, objs.Length); foreach (object obj in objs) { ProcessObject(PSObjectHelper.AsPSObject(obj)); } } break; default: { // do not enumerate at all (CoreOnly) ProcessObject(so); } break; } }
/// <summary> /// Output a "method returning" debug message and also dumps the .ToString() representation /// of the val object. /// </summary> /// <param name="logger"> /// The logger to log to /// </param> /// <param name="expandEnumerables"> /// If EnumerableExpansion.Expand and the val parameter is an IEnumerable then we'll print .ToString for every /// item in the collection and not just the collection itself. /// </param> /// <param name="val"> /// The value that is being returned that should be logged. /// </param> public static void DebugMethodReturning(this ILog logger, EnumerableExpansion expandEnumerables, object val) { #region Input validation Insist.IsNotNull(logger, "logger"); Insist.IsDefined <EnumerableExpansion>(expandEnumerables, "expandEnumerables"); #endregion if (logger.IsDebugEnabled) { logger.Debug(BuildMessage(logger, string.Format(METHOD_RETURNING_PATTERN, GetCallingMethod()), expandEnumerables, new object[] { val })); } }
/// <summary> /// Dumps the .ToString() representation of each of the supplied arguments /// </summary> /// <param name="logger"> /// The logger to write to /// </param> /// <param name="expandEnumerables"> /// If EnumerableExpansion.Expand and the args collection contains any IEnumerables then we'll print .ToString for every /// item in the collection and not just the collection itself. /// </param> /// <param name="args"> /// The list of arguments that need to be logged. /// </param> public static void DebugDumpArguments(this ILog logger, EnumerableExpansion expandEnumerables, params object[] args) { #region Input validation Insist.IsNotNull(logger, "logger"); Insist.IsDefined <EnumerableExpansion>(expandEnumerables, "expandEnumerables"); #endregion if (logger.IsDebugEnabled) { logger.Debug(BuildMessage(logger, string.Empty, expandEnumerables, args)); } }
/// <summary> /// Output a "method called" debug message and also dumps the .ToString() representation /// of all objects supplied in the args array. /// </summary> /// <param name="logger"> /// The logger to log to /// </param> /// <param name="expandEnumerables"> /// If EnumerableExpansion.Expand and the args collection contains any IEnumerables then we'll print .ToString for every /// item in the collection and not just the collection itself. /// </param> /// <param name="args"> /// The list of arguments that need to be logged /// </param> public static void DebugMethodCalled(this ILog logger, EnumerableExpansion expandEnumerables, params object[] args) { #region Input validation Insist.IsNotNull(logger, "logger"); Insist.IsDefined <EnumerableExpansion>(expandEnumerables, "expandEnumerables"); #endregion if (logger.IsDebugEnabled) { logger.Debug(BuildMessage(logger, string.Format(METHOD_CALLED_PATTERN, GetCallingMethod()), expandEnumerables, args)); } }
internal static bool Convert(string expansionString, out EnumerableExpansion expansion) { expansion = EnumerableExpansion.EnumOnly; if (string.Equals(expansionString, "CoreOnly", StringComparison.OrdinalIgnoreCase)) { expansion = EnumerableExpansion.CoreOnly; return(true); } if (string.Equals(expansionString, "EnumOnly", StringComparison.OrdinalIgnoreCase)) { expansion = EnumerableExpansion.EnumOnly; return(true); } if (string.Equals(expansionString, "Both", StringComparison.OrdinalIgnoreCase)) { expansion = EnumerableExpansion.Both; return(true); } return(false); }
internal static bool Convert(string expansionString, out EnumerableExpansion expansion) { expansion = EnumerableExpansion.EnumOnly; if (String.Equals(expansionString, CoreOnlyString, StringComparison.OrdinalIgnoreCase)) { expansion = EnumerableExpansion.CoreOnly; return true; } if (String.Equals(expansionString, EnumOnlyString, StringComparison.OrdinalIgnoreCase)) { expansion = EnumerableExpansion.EnumOnly; return true; } if (String.Equals(expansionString, BothString, StringComparison.OrdinalIgnoreCase)) { expansion = EnumerableExpansion.Both; return true; } return false; }
/// <summary> /// Builds up the message that will get logged /// </summary> private static string BuildMessage(ILog logger, string prefix, EnumerableExpansion expandEnumerables, object[] args) { #region Input validation prefix = (prefix == null ? string.Empty : prefix); if (args == null || args.Length == 0) { return(prefix + NO_ARGUMENTS); } #endregion StringBuilder builder = new StringBuilder(); builder.Append(prefix); object currentObject = null; for (int i = 0; i < args.Length; i++) { currentObject = args[i]; if (i > 0) { builder.Append(ARGUMENT_SEPERATOR); } if (currentObject == null) { builder.Append(NULL_ARGUMENT); } else { //See if the current object has a renderer associated with it. IObjectRenderer renderer = logger.Logger.Repository.RendererMap.Get(currentObject.GetType()); bool useRenderer = (renderer != null); if (expandEnumerables == EnumerableExpansion.DoNotExpand && renderer is DefaultRenderer && currentObject is IEnumerable) //The default renderer will expand enumerbales for us, if this //method was called explicitly stating not to expand the enumerables //then we'll not use the default renderer and just dump out the ToString() { useRenderer = false; } if (currentObject is string) //Strings are also enumerables but we don't want them //to get expanded. { useRenderer = false; } if (useRenderer) //We've got a renderer, this is the preferred way of dumping //out an object { using (TextWriter writer = new StringWriter()) { renderer.RenderObject(logger.Logger.Repository.RendererMap, currentObject, writer); builder.Append(writer.ToString()); } } else //No renderer available, just dump out the ToString of the object. { builder.Append(string.Format(ARGUMENT_PATTERN, currentObject.ToString())); } } } return(builder.ToString()); }
/// <summary> /// Builds up the message that will get logged /// </summary> private static string BuildMessage(ILog logger, string prefix, EnumerableExpansion expandEnumerables, object[] args) { #region Input validation prefix = (prefix == null ? string.Empty : prefix); if (args == null || args.Length == 0) { return prefix + NO_ARGUMENTS; } #endregion StringBuilder builder = new StringBuilder(); builder.Append(prefix); object currentObject = null; for (int i = 0; i < args.Length; i++) { currentObject = args[i]; if (i > 0) { builder.Append(ARGUMENT_SEPERATOR); } if (currentObject == null) { builder.Append(NULL_ARGUMENT); } else { //See if the current object has a renderer associated with it. IObjectRenderer renderer = logger.Logger.Repository.RendererMap.Get(currentObject.GetType()); bool useRenderer = (renderer != null); if (expandEnumerables == EnumerableExpansion.DoNotExpand && renderer is DefaultRenderer && currentObject is IEnumerable) //The default renderer will expand enumerbales for us, if this //method was called explicitly stating not to expand the enumerables //then we'll not use the default renderer and just dump out the ToString() { useRenderer = false; } if (currentObject is string) //Strings are also enumerables but we don't want them //to get expanded. { useRenderer = false; } if (useRenderer) //We've got a renderer, this is the preferred way of dumping //out an object { using (TextWriter writer = new StringWriter()) { renderer.RenderObject(logger.Logger.Repository.RendererMap, currentObject, writer); builder.Append(writer.ToString()); } } else //No renderer available, just dump out the ToString of the object. { builder.Append(string.Format(ARGUMENT_PATTERN, currentObject.ToString())); } } } return builder.ToString(); }
/// <summary> /// Dumps the .ToString() representation of each of the supplied arguments /// </summary> /// <param name="logger"> /// The logger to write to /// </param> /// <param name="expandEnumerables"> /// If EnumerableExpansion.Expand and the args collection contains any IEnumerables then we'll print .ToString for every /// item in the collection and not just the collection itself. /// </param> /// <param name="args"> /// The list of arguments that need to be logged. /// </param> public static void DebugDumpArguments(this ILog logger, EnumerableExpansion expandEnumerables, params object[] args) { #region Input validation Insist.IsNotNull(logger, "logger"); Insist.IsDefined<EnumerableExpansion>(expandEnumerables, "expandEnumerables"); #endregion if (logger.IsDebugEnabled) { logger.Debug(BuildMessage(logger, string.Empty, expandEnumerables, args)); } }
/// <summary> /// Output a "method returning" debug message and also dumps the .ToString() representation /// of the val object. /// </summary> /// <param name="logger"> /// The logger to log to /// </param> /// <param name="expandEnumerables"> /// If EnumerableExpansion.Expand and the val parameter is an IEnumerable then we'll print .ToString for every /// item in the collection and not just the collection itself. /// </param> /// <param name="val"> /// The value that is being returned that should be logged. /// </param> public static void DebugMethodReturning(this ILog logger, EnumerableExpansion expandEnumerables, object val) { #region Input validation Insist.IsNotNull(logger, "logger"); Insist.IsDefined<EnumerableExpansion>(expandEnumerables, "expandEnumerables"); #endregion if (logger.IsDebugEnabled) { logger.Debug(BuildMessage(logger, string.Format(METHOD_RETURNING_PATTERN, GetCallingMethod()), expandEnumerables, new object[] { val })); } }
/// <summary> /// Output a "method called" debug message and also dumps the .ToString() representation /// of all objects supplied in the args array. /// </summary> /// <param name="logger"> /// The logger to log to /// </param> /// <param name="expandEnumerables"> /// If EnumerableExpansion.Expand and the args collection contains any IEnumerables then we'll print .ToString for every /// item in the collection and not just the collection itself. /// </param> /// <param name="args"> /// The list of arguments that need to be logged /// </param> public static void DebugMethodCalled(this ILog logger, EnumerableExpansion expandEnumerables, params object[] args) { #region Input validation Insist.IsNotNull(logger, "logger"); Insist.IsDefined<EnumerableExpansion>(expandEnumerables, "expandEnumerables"); #endregion if (logger.IsDebugEnabled) { logger.Debug(BuildMessage(logger, string.Format(METHOD_CALLED_PATTERN, GetCallingMethod()), expandEnumerables, args)); } }