/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { double doubleValue = (double)value; if (double.IsPositiveInfinity(doubleValue)) { return(typeof(double).Name + "." + nameof(double.PositiveInfinity)); } if (double.IsNegativeInfinity(doubleValue)) { return(typeof(double).Name + "." + nameof(double.NegativeInfinity)); } if (double.IsNaN(doubleValue)) { return(doubleValue.ToString(CultureInfo.InvariantCulture)); } string formattedValue = doubleValue.ToString("R", CultureInfo.InvariantCulture); return((formattedValue.IndexOf('.') == -1) && (formattedValue.IndexOf('E') == -1) ? formattedValue + ".0" : formattedValue); }
public string Format(object value, FormattingContext context, FormatChild formatChild) { var prefix = context.UseLineBreaks ? Environment.NewLine : string.Empty; var diagnostic = (Diagnostic)value; return($"{prefix}\"[{diagnostic.Code} ({diagnostic.Level})] {diagnostic.Message}\""); }
/// <summary> /// Returns a <see cref="string" /> that represents this instance. /// </summary> /// <param name="value">The value for which to create a <see cref="string"/>.</param> /// <param name="useLineBreaks"> </param> /// <param name="processedObjects"> /// A collection of objects that /// </param> /// <param name="nestedPropertyLevel"> /// The level of nesting for the supplied value. This is used for indenting the format string for objects that have /// no <see cref="object.ToString()"/> override. /// </param> /// <returns> /// A <see cref="string" /> that represents this instance. /// </returns> public string Format(object value, FormattingContext context, FormatChild formatChild) { var jToken = (JToken)value; string result = context.UseLineBreaks ? jToken?.ToString(Newtonsoft.Json.Formatting.Indented) : jToken?.ToString().RemoveNewLines(); return(result ?? "<null>"); }
private string GetTypeAndMemberValues(object obj, FormattingContext context, FormatChild formatChild) { var builder = new StringBuilder(); if (context.Depth == RootLevel) { builder.AppendLine(); builder.AppendLine(); } Type type = obj.GetType(); builder.AppendLine(TypeDisplayName(type)); builder.Append(CreateWhitespaceForLevel(context.Depth)).Append('{').AppendLine(); IEnumerable <SelectedMemberInfo> members = GetMembers(type); foreach (SelectedMemberInfo memberInfo in members.OrderBy(mi => mi.Name)) { string memberValueText = GetMemberValueTextFor(obj, memberInfo, context, formatChild); builder.AppendLine(memberValueText); } builder.Append(CreateWhitespaceForLevel(context.Depth)).Append('}'); return(builder.ToString()); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var timeSpan = (TimeSpan)value; if (timeSpan == TimeSpan.MinValue) { return("min time span"); } if (timeSpan == TimeSpan.MaxValue) { return("max time span"); } List <string> fragments = GetNonZeroFragments(timeSpan); if (!fragments.Any()) { return("default"); } string sign = (timeSpan.Ticks >= 0) ? "" : "-"; if (fragments.Count == 1) { return(sign + fragments.Single()); } else { return(sign + JoinUsingWritingStyle(fragments)); } }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { string prefix = context.UseLineBreaks ? Environment.NewLine : ""; string escapedString = value.ToString().Escape(); return(prefix + "\"" + escapedString + "\""); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var document = (XDocument)value; return((document.Root != null) ? formatChild("root", document.Root) : FormatDocumentWithoutRoot()); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { MethodInfo method = GetFormatter(value); object[] parameters = new[] { value }; return((string)method.Invoke(null, parameters)); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var element = (XElement)value; return(element.HasElements ? FormatElementWithChildren(element) : FormatElementWithoutChildren(element)); }
public string Format(object value, FormattingContext context, FormatChild formatChild) { string newline = context.UseLineBreaks ? Environment.NewLine : ""; string padding = new string('\t', context.Depth); var token = (IToken)value; return($"{newline}{padding} {token.TokenType} {token.Value} ({token.StartPosition.Line} {token.StartPosition.Column}) - ({token.EndPosition.Line} {token.EndPosition.Column})"); }
public string Format(object value, FormattingContext context, FormatChild formatChild) { if (value is Task task) { return($"{formatChild("type", task.GetType())} {{Status={task.Status}}}"); } else { return("<null>"); } }
public string Format(object value, FormattingContext context, FormatChild formatChild) { var task = value as Task; if (!ReferenceEquals(task, null)) { return($"{formatChild("type", task.GetType())} {{Status={task.Status}}}"); } else { return("<null>"); } }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { string outerXml = ((XmlNode)value).OuterXml; const int maxLength = 20; if (outerXml.Length > maxLength) { outerXml = outerXml.Substring(0, maxLength).TrimEnd() + "…"; } return(outerXml.Escape(escapePlaceholders: true)); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var response = (HttpResponseMessage)value; var messageBuilder = new StringBuilder(); messageBuilder.AppendLine(); messageBuilder.AppendLine(); messageBuilder.AppendLine("The HTTP response was:"); Func <Task> contentResolver = async() => await AppendHttpResponseMessage(messageBuilder, response); contentResolver.ExecuteInDefaultSynchronizationContext().GetAwaiter().GetResult(); return(messageBuilder.ToString()); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { if (value.GetType() == typeof(object)) { return(string.Format("System.Object (HashCode={0})", value.GetHashCode())); } string prefix = context.UseLineBreaks ? Environment.NewLine : string.Empty; if (HasDefaultToStringImplementation(value)) { return(prefix + GetTypeAndMemberValues(value, context, formatChild)); } return(prefix + value); }
public string Format(object value, FormattingContext context, FormatChild formatChild) { var assertionsFailures = (AssertionsFailures)value; var messageBuilder = new StringBuilder(); messageBuilder.AppendLine(); messageBuilder.AppendLine(); foreach (var failure in assertionsFailures.FailuresMessages) { messageBuilder.AppendLine($" - { failure.ReplaceFirstWithLowercase() }"); } return(messageBuilder.ToString()); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var exception = (Exception)value; var builder = new StringBuilder(); builder.AppendFormat("{0} with message \"{1}\"\n", exception.GetType().FullName, exception.Message); if (exception.StackTrace != null) { foreach (string line in exception.StackTrace.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) { builder.Append(" ").AppendLine(line); } } return(builder.ToString()); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var newline = context.UseLineBreaks ? Environment.NewLine : ""; var padding = new string('\t', context.Depth); var result = new StringBuilder($"{newline}{padding}{{"); foreach (DictionaryEntry entry in (IDictionary)value) { result.AppendFormat( "[{0}]: {{{1}}},", formatChild("Key", entry.Key), formatChild("Value", entry.Value)); } result.Append($"{newline}{padding}}}"); return(result.ToString()); }
public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild) { var assertionsFailures = (AssertionsFailures)value; var messageBuilder = new StringBuilder(); messageBuilder.AppendLine(); messageBuilder.AppendLine(); foreach (var failure in assertionsFailures.FailuresMessages) { messageBuilder.AppendLine($" - { failure.ReplaceFirstWithLowercase() }"); } formattedGraph.AddFragment(messageBuilder.ToString()); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { float singleValue = (float)value; if (float.IsPositiveInfinity(singleValue)) { return(typeof(float).Name + "." + nameof(float.PositiveInfinity)); } if (float.IsNegativeInfinity(singleValue)) { return(typeof(float).Name + "." + nameof(float.NegativeInfinity)); } if (float.IsNaN(singleValue)) { return(singleValue.ToString(CultureInfo.InvariantCulture)); } return(singleValue.ToString("R", CultureInfo.InvariantCulture) + "F"); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { ICollection <object> enumerable = ((IEnumerable)value).ConvertOrCastToCollection <object>(); if (enumerable.Any()) { string postfix = string.Empty; if (enumerable.Count > MaxItems) { postfix = $", …{enumerable.Count - MaxItems} more…"; enumerable = enumerable.Take(MaxItems).ToArray(); } return("{" + string.Join(", ", enumerable.Select((item, index) => formatChild(index.ToString(), item))) + postfix + "}"); } else { return("{empty}"); } }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var enumerable = ((IEnumerable)value).Cast <object>().ToArray(); if (enumerable.Any()) { string postfix = ""; int maxItems = 32; if (enumerable.Length > maxItems) { postfix = $", …{enumerable.Length - maxItems} more…"; enumerable = enumerable.Take(maxItems).ToArray(); } return("{" + string.Join(", ", enumerable.Select((item, index) => formatChild(index.ToString(), item)).ToArray()) + postfix + "}"); } else { return("{empty}"); } }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { var exception = (AggregateException)value; if (exception.InnerExceptions.Count == 1) { return("(aggregated) " + formatChild("inner", exception.InnerException)); } else { var builder = new StringBuilder(); builder.AppendFormat("{0} (aggregated) exceptions:\n", exception.InnerExceptions.Count); foreach (Exception innerException in exception.InnerExceptions) { builder.AppendLine(); builder.AppendLine(formatChild("InnerException", innerException)); } return(builder.ToString()); } }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { return(value.ToString().Replace(" = ", " == ")); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { return(((uint)value).ToString(CultureInfo.InvariantCulture) + "u"); }
public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild) { var timeSpan = (TimeSpan)value; if (timeSpan == TimeSpan.MinValue) { formattedGraph.AddFragment("min time span"); return; } if (timeSpan == TimeSpan.MaxValue) { formattedGraph.AddFragment("max time span"); return; } List <string> fragments = GetNonZeroFragments(timeSpan); if (!fragments.Any()) { formattedGraph.AddFragment("default"); } string sign = (timeSpan.Ticks >= 0) ? string.Empty : "-"; if (fragments.Count == 1) { formattedGraph.AddFragment(sign + fragments.Single()); } else { formattedGraph.AddFragment(sign + fragments.JoinUsingWritingStyle()); } }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { return((context.UseLineBreaks ? Environment.NewLine : "") + "\"" + value.ToString().Escape() + "\""); }
/// <inheritdoc /> public string Format(object value, FormattingContext context, FormatChild formatChild) { return("0x" + ((byte)value).ToString("X2", CultureInfo.InvariantCulture)); }
private static string Format(object value, FormattingContext context, FormatChild formatChild) { IValueFormatter firstFormatterThatCanHandleValue = Formatters.First(f => f.CanHandle(value)); return(firstFormatterThatCanHandleValue.Format(value, context, formatChild)); }
public void Format(object value, FormattedObjectGraph formattedGraph, FormattingContext context, FormatChild formatChild) { var exception = (AggregateException)value; if (exception.InnerExceptions.Count == 1) { formattedGraph.AddFragment("(aggregated) "); formatChild("inner", exception.InnerException, formattedGraph); } else { formattedGraph.AddLine(Invariant($"{exception.InnerExceptions.Count} (aggregated) exceptions:")); foreach (Exception innerException in exception.InnerExceptions) { formattedGraph.AddLine(string.Empty); formatChild("InnerException", innerException, formattedGraph); } } }