public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { IEnumerable <LogLineField> fields = value as IEnumerable <LogLineField>; if (fields != null && parameter != null) { LogLineField field = fields.FirstOrDefault(f => f.Name == parameter.ToString()); if (field != null) { return(field.Content); } return(false); } return(false); }
/// <summary> /// Receives the raw conversion pattern from the Log4Net config file, and generates the necessary <see cref="LogLineField"/> information. /// </summary> /// <param name="rawConversionPattern">The raw conversion pattern string.</param> private static List <LogLineField> GenerateFieldsFromConversionPattern(string rawConversionPattern) { List <LogLineField> newFields = new List <LogLineField>(); List <string> fieldPatterns = new List <string>(); int startPos = 0; bool inField = false; for (int i = 0; i < rawConversionPattern.Length; i++) { if ((rawConversionPattern[i] == ' ' || i == rawConversionPattern.Length - 1) && inField) { fieldPatterns.Add(rawConversionPattern.Substring(startPos, i - startPos).Trim()); inField = false; continue; } if (rawConversionPattern[i] != '%') { continue; } if (inField) { fieldPatterns.Add(rawConversionPattern.Substring(startPos, i - startPos).Trim()); inField = false; } startPos = i; inField = true; } foreach (string pattern in fieldPatterns) { string cleanedPattern = PatternLayout.CleanConversionPatternField(pattern); bool inPadding = false; bool inTruncate = false; string paddingElement = string.Empty; string truncateElement = string.Empty; string patternElement = string.Empty; foreach (char c in cleanedPattern) { if (c == '.') { inPadding = false; inTruncate = true; continue; } if (Char.IsDigit(c)) { if (!inTruncate) { inPadding = true; } else { truncateElement += c; } if (inPadding) { paddingElement += c; } continue; } inPadding = inTruncate = false; patternElement += c; } if (string.IsNullOrEmpty(patternElement)) { continue; } LogLineField newField = new LogLineField { Name = PatternLayout.RetrieveFieldNameFromConversionPattern(patternElement) }; if (!string.IsNullOrEmpty(truncateElement)) { newField.Type = LogLineType.FixedWidth; newField.FixedWidth = int.Parse(truncateElement); } else { newField.Type = LogLineType.Delimited; newField.EndingCharacter = ' '; if (!string.IsNullOrEmpty(paddingElement)) { newField.MinWidth = int.Parse(paddingElement); } } newFields.Add(newField); } return(newFields); }